• All, Gmail is currently rejecting messages from my host. I have a ticket in process, but it may take some time to resolve. Until further notice, do NOT use Gmail for your accounts. You will be unable to receive confirmations and two factor messages to login.

Memory Management Questions

Bruce

Active member
Hello everyone,

First of all, sorry if any of these questions has been asked before.
My project is very critical in terms of OOM issues that I have been having.
I love having more sprites for smooth animations, but it looks like I can't do that in my high-res OpenBor game anymore.
I don't have much problems with the slow down or frame rate problems, but the OOM is killing me, especially whenever I play webm videos.
I only load about 10-15% of sprites of what I intended to do, but it is already causing the OOM, and even OOM at the game logo screen!
Therefore, I really need to make sure I know what I am doing to avoid memory leakages as much as possible.
I have the followings questions below if you guys can help me, it would be greatly appreciated.

1. When I load the sprite with

(A)
Code:
setlocalvar("TitleScreenPNG",  loadsprite("data/bgs/Title.png"));
if I accidentally load it again without unload it first, will it overwrite it or add another one on top of it?

If I unload the sprite
with
Code:
    void titleScreenPNG = getlocalvar("TitleScreenPNG");
    if(titleScreenPNG)
    {
        free(titleScreenPNG);
        setlocalvar("TitleScreenPNG", NULL());
    }
Will this remove it completely off the memory?

(B)
If I am using the same sprites to create more entities, how would they affect the OOM?

I know that everytime you load a model into the engine, it costs memory, but I don't know by how much.

2. Scripts
(A)
I know I have way too many scripts that I will use them later, mostly a copycat from this forum.
For example,
If I have 50 scripts on 1 file, and I am only using only 20 of them, will the other 30 scripts affect the memory?

(B)
Right now I have 1 main script file (MainAnim_All.c) that includes all other scripts files inside like this:
Code:
#import "data/scripts/anim/MainAnim.c"
#import "data/scripts/anim/Mobility_Anim.c"
#import "data/scripts/anim/MpSmSpEx.c"
#import "data/scripts/anim/Spawns_Anim.c"
#import "data/scripts/anim/Grabs_Anim.c"
#include "data/scripts/common/constants.h"
Then I load this MainAnim_All.c in the entity's header.
What if the entity only needs 1 or 2 scripts from the Spawns_Anim.c,
should I load the Spawns_Anim.c instead of MainAnim_All.c in this entity's header?

3. killentity function
Code:
@cmd    killentity getlocalvar("self")
When I use this function, will it remove the spawned entity off the screen and also off the memory?
I know that once you load the entity model, all the sprites inside the model will be loaded to the memory forever until you manually remove them.
How do you know manually remove the sprites off the memory?

4. Sound and Music files
How would they affect the OOM?
I have a lot of sounds and music files.... so I am wondering if these actually affect OOM a lot or not.

5. High Definition Stage Backgrounds
I love high-res background pngs, but I don't know how much these would affect the OOM.
How would they affect the OOM?

Sorry for the noob questions, and thank you very much for your help in advance.
 
When I load the sprite with

(A)
setlocalvar("TitleScreenPNG", loadsprite("data/bgs/Title.png"));if I accidentally load it again without unload it first, will it overwrite it or add another one on top of it?

Neither. If that filename is already loaded, the engine ignores you and returns the pointer to existing asset.

(B)
If I am using the same sprites to create more entities, how would they affect the OOM?

I know that everytime you load a model into the engine, it costs memory, but I don't know by how much.

The engine knows to reuse assets, so they don't add more memory use at all. The model data itself needs memory of course, but its a very nominal amount. Maybe equal to about 1 sprite.

2. Scripts
(A)

I know I have way too many scripts that I will use them later, mostly a copycat from this forum.
For example,
If I have 50 scripts on 1 file, and I am only using only 20 of them, will the other 30 scripts affect the memory?

(B)
Right now I have 1 main script file (MainAnim_All.c) that includes all other scripts files inside like this:
#import "data/scripts/anim/MainAnim.c" #import "data/scripts/anim/Mobility_Anim.c" #import "data/scripts/anim/MpSmSpEx.c" #import "data/scripts/anim/Spawns_Anim.c" #import "data/scripts/anim/Grabs_Anim.c" #include "data/scripts/common/constants.h"Then I load this MainAnim_All.c in the entity's header.
What if the entity only needs 1 or 2 scripts from the Spawns_Anim.c,
should I load the Spawns_Anim.c instead of MainAnim_All.c in this entity's header?

Scripts always use memory, but use of #import mitigates that to once per script instance. There's no one single best way to optimize. Coding is an art form built on science. There are universal principals, but every module and every situation will have different specific needs.

3. killentity function
@cmd killentity getlocalvar("self")When I use this function, will it remove the spawned entity off the screen and also off the memory?
I know that once you load the entity model, all the sprites inside the model will be loaded to the memory forever until you manually remove them.
How do you know manually remove the sprites off the memory?

Yes, the entity is removed - but not its model (so you can spawn it later). You have to use loadmodel 3 IIRC, and that will unload the assets when the entity is removed from play.

4. Sound and Music files
How would they affect the OOM?
I have a lot of sounds and music files.... so I am wondering if these actually affect OOM a lot or not.

The background music uses very little memory because it is streamed. Sound effects do use memory, and once loaded, they stay in RAM until unloaded.

5. High Definition Stage Backgrounds
I love high-res background pngs, but I don't know how much these would affect the OOM.
How would they affect the OOM?

I already explained this to you using specific and very well defined math. Sprites are sprites. Doesn't matter what an image is loaded for. The only difference at all, is that layers are already removed (and if needed, loaded again) between levels.

DC
 
You have to use loadmodel 3 IIRC, and that will unload the assets when the entity is removed from play.
I won't reply on this. As @Kratus discovered using "load 3" with complex entities (entities with either scripts or anything that is not type panel or none) will result on a random silent crash - the engine closes without any trace on the log. This haunted me for years.

The solution, on this case, is to use "spawn entity 1" on the level file so it will get unloaded when the level ends
 
Scripts always use memory, but use of #import mitigates that to once per script instance. There's no one single best way to optimize. Coding is an art form built on science. There are universal principals, but every module and every situation will have different specific needs.
Greatly appreciated for your detailed information which is very helpful for me.
I do remember in another reply that you mentioned about using #import method.

Can you please explain little more on how scripts are being used in the OpenBor engine system?
1. Do scripts use and release memory after they finish everytime they are being called out/used?
For example, if I have this script (by Bloodbane, and I modified it a bit for my needs):
Code:
void SpawnByScreenRes(void EntityName, float fX, float fY, float fZ, int palette, int AnimID, int FacingDir)
{
    //FacingDir: 0 = opposite dir, 1 = same dir,
    //2 = always facing right, 3 = always facing left
    //Spawns entity based on left screen edge and z axis
    //Auto adjust with camera's position
    //EntityName: Model name of entity to be spawned in.
    //fX: X distance relative to left edge
    //fY: Y height from ground
    //fZ: Z coordinate

    void self = getlocalvar("self"); //Get calling entity.
    void vSpawn; //Spawn object.
    int SelfDir = getentityproperty(self, "direction");
    int XPos = openborvariant("xpos"); //Get screen edge's position
    int YPos = openborvariant("ypos"); // Get camera position
    int ScreenRes = openborvariant("hResolution"); // Get screen width/horizontal

    clearspawnentry(); //Clear current spawn entry.
    setspawnentry("name", EntityName); //Acquire spawn entity by name.
    if(palette != NULL()){setspawnentry("map", palette);} //DEFINE PALETTE.

    vSpawn = spawn(); //Spawn in entity.
   ....
   ....
    
    changeentityproperty(vSpawn, "position", fX + XPos, fZ + YPos, fY); //Set spawn location.
    changeentityproperty(vSpawn, "parent", self);   
    //SET CUSTOM ANIMATION
    if(AnimID != NULL()){performattack(vSpawn, AnimID);}
    
    return vSpawn; //Return spawn
}
and I use it in
anim freespecial1, 2, 3
This SpawnByScreenRes script only uses and releases the memory after it finishes whenever I perform freespecial1, 2, or 3, ,
is this correct?


Thank you vey much
 
I won't reply on this. As @Kratus discovered using "load 3" with complex entities (entities with either scripts or anything that is not type panel or none) will result on a random silent crash - the engine closes without any trace on the log. This haunted me for years.

The solution, on this case, is to use "spawn entity 1" on the level file so it will get unloaded when the level ends
1 is to unload the model and 2 is to unload the sprites.
So if I use "spawn "SummonFairy" 2"
it will unload all the the sprites from SummonFairy entity off the memory when the level ends, right?
Is there anyway to unload the sprites off the memory at anytime instead of waiting until the level ends?

What I am trying to do is that
I will have at least 10 summon entities and each of them has at least 250 sprites.
I set them as know in the model.c and load 1 summon entity in the char header per main character type.
Basically, I don't want to load them at all at once which it will crash the game with OOM.

But I want to be able to unload the summon entity sprites off the memory that are not being used - basically after the player selects a different main character.
Otherwise, if the player keeps on selecting a different main character after it dies, eventually all 10 summon entities' sprites will be all loaded.

Thank you so much
 
Last edited:
1 is to unload the model and 2 is to unload the sprites.
load {name} {bi}

  • Used to load a model whose name is {name} in current level.
  • {bi} determines if model stays in memory or not after current level ends.
    • 0 = Nothing. The model is left alone (IOW, remains loaded) and loop moves on. This is the default action.
    • 1 = Unload model only. The model is unloaded, but the sprites are not. Pretty pointless unless the model has a lot of script attached to it.
    • 2 = Unload sprites only. The mode remains loaded, but its sprites are removed from memory.
    • 3 = You'll notice there there is no third option, but because bitwise logic is in play, 3 effectively means "1 and 2" - model and sprites are unloaded.
So if I use "spawn "SummonFairy" 2"
I think you're confusing things. LOAD and SPAWN are different functions - there's no "2" option for SPAWN, just 0 and 1.

spawn {name} {bi}

  • {name} is the name of an entity defined in a .txt file which was loaded in MODELS.txt.
  • {bi} determines if model stays in memory or not after current level ends.
    • 0 = Model stays in memory.
    • 1 = Model is unloaded when current level ends.
  • {name} will be spawned (created) in current level. Where and with what attributes are determined by the next set of fields.

I see that you're having memory problems, but I already suspected that you were doing something wrong, and the confusion between LOAD and SPAWN reinforced that impression for me.

You need to be VERY careful when using scripts: if you don't know exactly what you're doing, you'll ruin your game. And it's not the script's or the engine's fault - it's like not knowing how to handle a sharp knife and blaming it when you cut yourself.

Is there anyway to unload the sprites off the memory at anytime instead of waiting until the level ends?
Yes (I keep forgot to put this on the manual) OpenBOR v3.0 Build 6412
but read @msmalik681 comment carefuly.
 
load {name} {bi}

  • Used to load a model whose name is {name} in current level.
  • {bi} determines if model stays in memory or not after current level ends.
    • 0 = Nothing. The model is left alone (IOW, remains loaded) and loop moves on. This is the default action.
    • 1 = Unload model only. The model is unloaded, but the sprites are not. Pretty pointless unless the model has a lot of script attached to it.
    • 2 = Unload sprites only. The mode remains loaded, but its sprites are removed from memory.
    • 3 = You'll notice there there is no third option, but because bitwise logic is in play, 3 effectively means "1 and 2" - model and sprites are unloaded.

I think you're confusing things. LOAD and SPAWN are different functions - there's no "2" option for SPAWN, just 0 and 1.

spawn {name} {bi}

  • {name} is the name of an entity defined in a .txt file which was loaded in MODELS.txt.
  • {bi} determines if model stays in memory or not after current level ends.
    • 0 = Model stays in memory.
    • 1 = Model is unloaded when current level ends.
  • {name} will be spawned (created) in current level. Where and with what attributes are determined by the next set of fields.

I see that you're having memory problems, but I already suspected that you were doing something wrong, and the confusion between LOAD and SPAWN reinforced that impression for me.

You need to be VERY careful when using scripts: if you don't know exactly what you're doing, you'll ruin your game. And it's not the script's or the engine's fault - it's like not knowing how to handle a sharp knife and blaming it when you cut yourself.


Yes (I keep forgot to put this on the manual) OpenBOR v3.0 Build 6412
but read @msmalik681 comment carefuly.
Yes, I did get mixed with the spawn and load functions. I asked the question on how to unload the model and the sprites, DC mentioned loadmodel entityname 3.
Then you mentioned this function has silence crash and you suggested with spawn entityname 1, so thought this is another function of it.
But I did understand the difference between load a model and spawn a model.
As far as the OOM problems, it is mostly because of the high quantity numbers of 1080p sprites.
There may be some other causes that I don't know about, but I am still in a very early stage in the game design.
I am still using the OpenBor template that I downloaded from this forum and still use the same stage backgrounds and enemy character files that came with the template although I have modified them a bit due to the sprite resizing to match with the 1080p setup.
Right now all I am doing is gathering the sprites, editing and adding them to the game.
On top of that, I also add more summon entities, and then test them with webm videos.
Basically so far I only use a few basically scripts (spawn script, playsample script, ) for these sprites.
I figured it is very tricky to make the game works in 1080p because all these sprites eat up all the RAM of my 5-6 years old non-gamer computer. But my goal is to design a game that computer like mine can play it.
This is why I need to learn how to optimize the scripts and make sure I understand what I am doing.

Thank you very much for your help
 
If your using openbor 4.0 you can use this command:

unload_model("model_name");

To unload a model at any time.
I have tried to run the build 4.0, but it closed without error log. I know I need to make changes in order to make it work, but I don't know what kind of changes that I need to make beside the attack format - attack 595 518 482 513 5 100 100 0 10 40
Does this unload_model("model_name"); remove both the model and its sprites off the memory?
I didn't know there was OpenBOR v3.0 Build 6412

Thank you.
 
6412 might also have the command try it and see and yes it will free the resources for the model but if you try to spawn the model again without loading it first then it may shut down.
 
6412 might also have the command try it and see and yes it will free the resources for the model but if you try to spawn the model again without loading it first then it may shut down.
The list in the link doesn't have Build 6412, am I missing something? I can create a simple script to check to make sure the model is loaded before spawn it.
Thank you
 
Not really you just need tight memory management. Also find a way to monitor the memory used for your game as you run to ensure there are no leaks.
 
Not really you just need tight memory management. Also find a way to monitor the memory used for your game as you run to ensure there are no leaks.
I have no idea how to monitor the memory usage. There is a debug option but this one is to check for the performance frame rate.
 
What operating system you using? If its windows then use the task manager. If linux htop there is also a gui application just dont have the name right now.
 
Back
Top Bottom