Talk:LlGetFreeMemory

From Second Life Wiki
Revision as of 17:53, 16 September 2007 by Ppaatt Lynagh (talk | contribs) (demo no heap garbage collected while entering state/ handling event)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Tips for Less Byte Code

Each script is presently allocated a 16K page of memory which contains the LSO bytecode, heap and stack. This function returns the difference between the current stack pointer and the heap's high-water mark. If the heap is exhausted, the stack and heap will collide and the script will crash.

If one is running low on memory, consider splitting the script into two parts or attempt to simplify the code. This includes strings as well (War and Peace should probably be placed in a notecard and not a string constant). Remember that every byte saved in code gives you one more for the heap and stack.

--Num Skall 15:54, 28 June 2007 (PDT)

Heap Garbage Often Not Collected

Hmmm.

It's surprisingly hard to prove that [llGetFreeMemory()] can return a negative value, thus correspondingly hard to prove that anybody is bothering to collect garbage from the heap.

The example below reports 365 of 16384 bytes (16 KiB) free for me ... and if instead I try { pairs(7) } in place of { pairs(6) } then I see "Script run-time error": "Stack-Heap Collision".

Feels like Second Life collects heap garbage only between states and events, not while entering a state or reacting to an event?

list pairs(integer power)
{
    list pairs = [0, 0];
    integer index;
    for (index = 0; index < power; ++index)
    {
        pairs += pairs;
    }
    return pairs;
}

integer tailDive(integer depth)
{
    if (depth <= 0)
    {
        return llGetFreeMemory();
    }
    return tailDive(depth - 1);
}

child()
{
        list entries = pairs(6); // 7 here gives "Stack-Heap Collision"
        entries = [];
}

default
{
    state_entry()
    {
        llOwnerSay(llGetScriptName() + ".default.state_entry");
        llOwnerSay((string) tailDive(12 * 1024 / 16) + " not stacked");
        llOwnerSay("Heaping ...");
        child();
        llOwnerSay("... heaped.");
        llOwnerSay((string) tailDive(12 * 1024 / 16) + " not stacked");
        llOwnerSay("");
    }
}

-- Ppaatt Lynagh 17:53, 16 September 2007 (PDT)