Difference between revisions of "LlGetFreeMemory"

From Second Life Wiki
Jump to navigation Jump to search
(include byte code in the stack/ heap model, link to "Script run-time error" "Stack-Heap Collision")
(See also: Talk:LlGetFreeMemory for dispute of the theory that llGetFreeMemory can return negative numbers.)
Line 17: Line 17:
These facts astonish new people who have not yet learned to think of this function as if its name were llGetNeverYetUsedMemory. These facts continue to astonish people who feel some LSL function should free all memory that doesn't now need to be in use and then also count bytes of memory now freed.<br/>
These facts astonish new people who have not yet learned to think of this function as if its name were llGetNeverYetUsedMemory. These facts continue to astonish people who feel some LSL function should free all memory that doesn't now need to be in use and then also count bytes of memory now freed.<br/>
<br/>
<br/>
See also: the "Script run-time error" "Stack-Heap Collision" among the [[LSL_Errors|LSL Errors]]
See also: the "Script run-time error" "Stack-Heap Collision" among the [[LSL_Errors|LSL Errors]]<br/>
See also: [[Talk:LlGetFreeMemory]] for dispute of the theory that llGetFreeMemory may return negative numbers
|constants
|constants
|examples=
|examples=

Revision as of 17:53, 16 September 2007

Summary

Function: integer llGetFreeMemory( );

Returns an integer that counts the bytes never yet used since reset in the free space of the task of the current script.

Caveats

  • Does not return the count of free bytes (surprise!).


We can concisely specify llGetFreeMemory in the context of the classic Unix model for a parallel task/ thread/ process. Think of the task of the script always holding 16384 bytes (16 KiB). Let the byte code and then stack grow up from the bottom, let the heap grow down from the top. llGetFreeMemory then returns the "historic lowest heap pointer minus the stack end pointer".

See the implications? llGetFreeMemory returns how many bytes of space were free, back when the heap was as large as the heap ever was, measured only since the last time when the script was reset. Mixing a partial history of the heap with the current state of the stack in this traditional Unix way can produce a positive or zero or negative return value, no matter how much freed memory the task now holds.

llGetFreeMemory does not count the bytes freed, llGetFreeMemory instead counts all the bytes never yet used.

These facts astonish new people who have not yet learned to think of this function as if its name were llGetNeverYetUsedMemory. These facts continue to astonish people who feel some LSL function should free all memory that doesn't now need to be in use and then also count bytes of memory now freed.

See also: the "Script run-time error" "Stack-Heap Collision" among the LSL Errors
See also: Talk:LlGetFreeMemory for dispute of the theory that llGetFreeMemory may return negative numbers

All Issues ~ Search JIRA for related Bugs

Examples

Calling llGetFreeMemory can look like this:

integer Ki = 1024; // 1024 == (1 << 10);
float maxPerScript = 16 * Ki;
llOwnerSay((string) ((maxPerScript - llGetFreeMemory())/Ki) + " KiB of memory used once or more by this script since reset");
llOwnerSay((string) ((maxPerScript - llGetFreeMemory())/Ki) + " KiB of memory used once or more by this script since reset");

Chat showing that the heap grows over time after shrinking at reset can look like this:

0.508789 KiB of memory used once or more by this script since reset
0.524414 KiB of memory used once or more by this script since reset

Shrinking the heap astonishingly does not reduce the value returned by llGetFreeMemory:

default
{
    state_entry()
    {
        llSay(0,"llGetFreeMemory() returned: "+(string)llGetFreeMemory()+"byte(s)");
        //outputs what llGetFreeMemory() returns in bytes
        if(TRUE)
        {
            list TEST1;
            TEST1=[1,5334,"Blah, blah, blah",<345,3.78,34>,<0,0,0,1>,"TEST"];
            TEST1=TEST1+TEST1+llGetFreeMemory();
            integer i;
            for(i=0;i<llGetListLength(TEST1);i++)
            {
                llSay(0,"Item number "+(string)i+" in the list is: "+llList2String(TEST1,i));
            }
            TEST1 = [];
        }
        llSay(0,"List deleted!");
        llSay(0,"Now llGetFreeMemory returns: "+(string)llGetFreeMemory());
    }
} // http://wiki.secondlife.com/wiki/User:TxMasterG_Ping/llGetFreeMemory

Notes

This function does not count free memory, and the name of this function makes this function difficult for people to learn. People say this function may be redefined or superseded by another more useful function when the LSL VM moves to Mono.

Deep Notes

Search JIRA for related Issues

Signature

function integer llGetFreeMemory();