Difference between revisions of "LlGetFreeMemory"

From Second Life Wiki
Jump to navigation Jump to search
(total rewrite to get the "Does not return the count of free bytes (surprise!)" point across)
Line 18: Line 18:
|constants
|constants
|examples=
|examples=
Calling llGetFreeMemory can look like this:
<pre>
<pre>
integer Ki = 1024; // 1024 == (1 << 10);
integer Ki = 1024; // 1024 == (1 << 10);
float maxPerScript = 16 * Ki;
float maxPerScript = 16 * Ki;
llOwnerSay((string) ((maxPerScript - llGetFreeMemory())/Ki) + " KiB of memory used once or more by this script");
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");
llOwnerSay((string) ((maxPerScript - llGetFreeMemory())/Ki) + " KiB of memory used once or more by this script since reset");
</pre>
</pre>
Chat showing that the heap grows over time after shrinking at reset can look like this:
Chat showing that the heap grows over time after shrinking at reset can look like this:
<pre>
<pre>
0.518555 KiB of memory written already now by this script
0.508789 KiB of memory used once or more by this script since reset
0.534180 KiB of memory written already now by this script</pre>
0.524414 KiB of memory used once or more by this script since reset
<br/>
</pre>
*Shrinking the heap astonishingly does not reduce the value returned by llGetFreeMemory
Shrinking the heap astonishingly does not reduce the value returned by llGetFreeMemory:
**[[User:TxMasterG Ping/llGetFreeMemory|Shrink the heap example]]
<pre>
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
</pre>
|helpers
|helpers
|also_functions
|also_functions

Revision as of 05:43, 16 September 2007

Summary

Function: integer llGetFreeMemory( );
0.0 Forced Delay
10.0 Energy

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 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.

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 Mono.

Deep Notes

Signature

function integer llGetFreeMemory();