Skidz' Thoughts

A simple place for me to write about my thoughts

Script Tricks for Memory

Well with the new site I figured it would be a good idea to at least get the ball rolling on some good content.

Not to long ago I posted on my twitter a useful little code snippet. One I use all the time.

llSetMemoryLimit(llRound((float)llGetUsedMemory() * 1.1));

It is a simple script command that will set the memory. But really, it is not the entire story, theres other things you need to do to help keep your scripts memory usage low.

Most people don’t know that when a region restarts, a user re-logs, or teleports the scripts memory allocation will rise back up. This command must be issued over and over because of this.

Here is an example of a simple script that does just this:

default
{
    state_entry()
    {
        llSetMemoryLimit(llRound((float)llGetUsedMemory() * 1.1));
    }
    
    changed(integer change)
    {
        if (change & (CHANGED_OWNER | CHANGED_REGION_START | CHANGED_REGION | CHANGED_TELEPORT))
            llSetMemoryLimit(llRound((float)llGetUsedMemory() * 1.1));
    }
}

Another neat trick

You can always increase the script memory again like so.

llSetMemoryLimit(65536);

Useful code-behind for memory

I take full advantage of Firestorm LSL Preprocessor functionality and have an include file that I keep this bit of code in.

#define S(pass_anything) (string)pass_anything
#define MemoryFull() llSetMemoryLimit(65536)
#define MemoryDetails() S(llGetUsedMemory()) + " out of " + S(llGetMemoryLimit()) + " saving " + S((65536 - llGetMemoryLimit()))
#define MemoryDetailsSay() llSay(0,MemoryDetails())
#define MemoryGeneric() MemoryMin(1024, 128); llSetObjectDesc(MemoryDetails())
#define EventChangedMemory(pass_integer,pass_integer2) changed(integer change){if (change & (CHANGED_OWNER | CHANGED_REGION_START | CHANGED_REGION | CHANGED_TELEPORT)) MemoryMin(pass_integer,pass_integer2);}
MemoryMin(integer moremem, integer amount2add){if (!llSetMemoryLimit(llGetUsedMemory() + moremem)) MemoryMin(moremem + amount2add, amount2add);}

An example of using this in your script would be:

#define S(pass_anything) (string)pass_anything
#define MemoryFull() llSetMemoryLimit(65536)
#define MemoryDetails() S(llGetUsedMemory()) + " out of " + S(llGetMemoryLimit()) + " saving " + S((65536 - llGetMemoryLimit()))
#define MemoryDetailsSay() llSay(0,MemoryDetails())
#define MemoryGeneric() MemoryMin(1024, 128); llSetObjectDesc(MemoryDetails())
#define EventChangedMemory(pass_integer,pass_integer2) changed(integer change){if (change & (CHANGED_OWNER | CHANGED_REGION_START | CHANGED_REGION | CHANGED_TELEPORT)) MemoryMin(pass_integer,pass_integer2);}
MemoryMin(integer moremem, integer amount2add){if (!llSetMemoryLimit(llGetUsedMemory() + moremem)) MemoryMin(moremem + amount2add, amount2add);}

default
{
    state_entry()
    {
        MemoryGeneric();
    }
    
    EventChangedMemory(1024,128)
}

While that might not look like valid script, with code-behind is it.

So when is it a good idea to use this in my script?

If your script is small and doesn’t collect information, than this is the perfect script to use this. There are exceptions, but for the most part this will work great for you.

So when do I not want to use this in my script?

If your script collects information, or takes in large amounts of data at a time, this is not a good candidate. You can always just try it, and if it doesn’t work well, remove it.

Closing notes

Managing your memory in your scripts is important. It gives us all a better experience.


Share