Skip to content
lsl.devlsl.devLSL Dev

llGetFreeMemory

function
Function syntax
integer llGetFreeMemory();
Returns
integer
Energy
10

Returns an integer representing the number of free bytes of memory currently available to the script.

How to use
integer result = llGetFreeMemory();

This function’s behavior is dependent upon the VM the script is using. Mono is the new VM, LSO is the old VM. The big difference between Mono and LSO is that Mono scripts run faster and can utilize four times more memory.

In Mono the value returned is the amount of free memory available to the script prior to garbage collection being run. This means that memory that is awaiting garbage collection counts against the scripts 64KiB allotment.

In addition to this, Mono does not enforce the memory restrictions as strictly as the LSO VM did12. (With LSO it was impossible to exceed the 16KiB memory cap.)

In LSO, the value returned by this function is the amount of memory that the Stack can use of the Heap has yet to allocate for itself.

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 (16KiB).

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 (due to their size). There is no defragment function3 but there are scripting techniques that can be used to reduce fragmentation.

  • The number of free bytes the Heap can use may be greater but not less.
  • “the number of free bytes of memory the script can use” means that if a memory limit is set (via llSetMemoryLimit), this function will report the free space between the script’s current memory usage and the defined memory limit, not the uncapped memory limit.
Calling llGetFreeMemory can look like this:
llOwnerSay((string)llGetFreeMemory() + " bytes of free memory available for allocation.");

The amount of free memory is updated only at the start of a server frame, meaning the values from this function can fluctuate. To ensure stable values, use a single-frame sleep, e.g. llSleep(0.01) before the call to llGetFreeMemory.

Original wiki source

Some wiki templates and tables need their original context. View this article on the Second Life Wiki. Technical wording and examples are retained from the source; historical guidance may differ from current behavior.

From the Second Life Wiki

Community documentation adapted from LlGetFreeMemory, by Linden Research, Inc. and contributing residents.View contributors and history.

Imported wiki revision:

Licensed under CC BY-SA 3.0. Last wiki edit: .Formatting and links have been adapted for this site; API metadata follows the LSL definitions.

  1. http://www.langnetsymposium.com/2009/talks/17-JimPurbrick-SecondLife.html ↩

  2. In LSO, all types are immutable, every operation results in Heap values being duplicated ↩

  3. Due to the design of the LSO VM, a defragment function is impossible. ↩