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 3: Line 3:
|func=llGetFreeMemory|sort=GetFreeMemory
|func=llGetFreeMemory|sort=GetFreeMemory
|return_type=integer
|return_type=integer
|func_footnote='''BUG:''' Instead of returning the free space it returns the amount of free space that has ''never'' been allocated by the heap (historic lowest heap pointer minus the stack end pointer). This means that the heap will appear to never shrink in size (when in fact it does grow and shrink to fill the free space).
|func_desc
|func_desc
|return_text=that is the available free space for the current script.
|return_text=that counts the bytes never yet used since reset in the free space of the task of the current script.
|spec
|spec
|caveats=*The return of this function can be wrong.
|caveats=
**[[User:TxMasterG Ping/llGetFreeMemory|Example]]
*Does not return the count of free bytes (surprise!).<br/>
<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 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/>
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.<br/>
<br/>
llGetFreeMemory does not count the bytes freed, llGetFreeMemory instead counts all the bytes never yet used.<br/>
<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.
|constants
|constants
|examples=
|examples=
<pre>
<pre>
// To show usage of memory by a script, take the free memory from 16k
integer Ki = 1024; // 1024 == (1 << 10);
llOwnerSay("Script uses " + (string)((16384 - llGetFreeMemory())/1024) + " kBytes");
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");
</pre>
</pre>
Chat showing that the heap grows over time after shrinking at reset can look like this:
<pre>
0.518555 KiB of memory written already now by this script
0.534180 KiB of memory written already now by this script</pre>
<br/>
*Shrinking the heap astonishingly does not reduce the value returned by llGetFreeMemory
**[[User:TxMasterG Ping/llGetFreeMemory|Shrink the heap example]]
|helpers
|helpers
|also_functions
|also_functions
Line 20: Line 36:
|also_tests
|also_tests
|also_articles
|also_articles
|notes=Because of the implementation of this function it's usefulness is limited. It has been stated that when the LSL VM is moved to [[Mono]] that this function will work properly.
|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]].
|permission
|permission
|negative_index
|negative_index

Revision as of 05:34, 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

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");
llOwnerSay((string) ((maxPerScript - llGetFreeMemory())/Ki) + " KiB of memory used once or more by this script");

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

0.518555 KiB of memory written already now by this script
0.534180 KiB of memory written already now by this script


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();