Difference between revisions of "LlGetFreeMemory"

From Second Life Wiki
Jump to navigation Jump to search
m
Line 59: Line 59:
|also_events
|also_events
|also_tests
|also_tests
|also_articles=
|also_articles
*The "Script run-time error" "Stack-Heap Collision" among the [[LSL Errors]].
*[[Talk:LlGetFreeMemory]] for dispute of the theory that llGetFreeMemory may return negative numbers.
|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]].
|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]].
<br/>
<br/>
Line 67: Line 65:
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".<br/>
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".<br/>
<br/>
<br/>
llGetFreeMemory does not count the bytes freed, llGetFreeMemory instead counts all the bytes never yet used.
llGetFreeMemory does not count the bytes freed, llGetFreeMemory instead counts all the bytes never yet used..<br/>
<br/>
See also: the "Script run-time error" "Stack-Heap Collision" among the [[LSL Errors]]<br/>
See also: [[Talk:LlGetFreeMemory]] for dispute of the theory that llGetFreeMemory may return negative numbers
|cat1=Script
|cat1=Script
|cat2=FixMe
|cat2=FixMe

Revision as of 06:24, 10 May 2009

Note!

Please notice that this article or section contains out-of-date information. You can help to improve the article by editing and updating it.

[[Category:Articles in need of updating|]]

Summary

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

Returns an integer that is the number of free bytes the Stack can use.

Specification

The LSL memory space is divided into four sections: Byte-code, Stack, Free Memory, Heap. Free Memory isn't an allocated block of memory, it's just the space between Stack and Heap. The size of all four sections combined is 16384 bytes.

Strings, lists and keys are stored in the Heap. Heap pointers (for strings, lists & keys), integers, floats, vectors and rotations are all temporarily stored in the stack as the script executes.

As the script executes the Stack grows and shrinks in size depending upon the complexity of the expressions being executed. Likewise the Heap grows as the script executes but unlike the stack, it never shrinks in size. When there is no free memory left for Stack or Heap to use they collide and a Stack-Heap Collision error is thrown causing the script to crash.

The Heap can become fragmented and blocks of memory in it can become unusable. There is no defragment function but there are scripting techniques that can be used to reduce fragmentation.

Caveats

  • The number of free bytes the Heap can use may be greater but not less.

Examples

Calling llGetFreeMemory can look like this: <lsl> 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"); </lsl> 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 increase the value returned by llGetFreeMemory: <lsl> 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

</lsl>

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.

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

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

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

Deep Notes

Signature

function integer llGetFreeMemory();