Difference between revisions of "User:Becky Pippen/Measure Script Memory Usage"

From Second Life Wiki
Jump to: navigation, search
(preliminary page related to upcoming script memory limits)
 
m
Line 13: Line 13:
 
Here is an LSL statement that reports how much free memory you have at the moment it's called:
 
Here is an LSL statement that reports how much free memory you have at the moment it's called:
  
llOwnerSay((string)llGetFreeMemory() + " bytes free");
+
<lsl>llOwnerSay((string)llGetFreeMemory() + " bytes free");</lsl>
  
 
To report how much memory is used (instead of free) in a Mono script, subtract
 
To report how much memory is used (instead of free) in a Mono script, subtract
 
free from total memory:
 
free from total memory:
  
llOwnerSay((string)(65536 - llGetFreeMemory()) + " bytes used");
+
<lsl>llOwnerSay((string)(65536 - llGetFreeMemory()) + " bytes used");</lsl>
  
 
For LSL-compiled scripts, change 65536 to 16384.
 
For LSL-compiled scripts, change 65536 to 16384.
  
It makes a difference where you place llGetFreeMemory() in your script. To see how
+
It makes a difference where you place [[llGetFreeMemory]]() in your script. To see how
 
much memory is used by the script's program code and static (pre-defined) variables,
 
much memory is used by the script's program code and static (pre-defined) variables,
put llGetFreeMemory() as the first line in state_entry() or on_rez() in the default
+
put llGetFreeMemory() as the first line in [[state_entry]]() or [[on_rez]]() in the default
 
state. To see how much memory your script used including all runtime dynamic data,
 
state. To see how much memory your script used including all runtime dynamic data,
 
run llGetFreeMemory() as the last statement executed after your script has created
 
run llGetFreeMemory() as the last statement executed after your script has created
Line 42: Line 42:
 
measure:
 
measure:
  
aList += "hello";  // appends one 5-char string to the list
+
<lsl>aList += "hello";  // appends one 5-char string to the list</lsl>
  
 
To measure the memory used by this statement, we'll average the memory consumption over a hundred loops.
 
To measure the memory used by this statement, we'll average the memory consumption over a hundred loops.
Line 49: Line 49:
 
or function calls made except the code being measured:
 
or function calls made except the code being measured:
  
 +
<lsl>
 
  list aList;
 
  list aList;
 
  integer startFreeMem;
 
  integer startFreeMem;
Line 67: Line 68:
 
         llOwnerSay("Change in free mem: " + (string)(startFreeMem - endFreeMem));
 
         llOwnerSay("Change in free mem: " + (string)(startFreeMem - endFreeMem));
 
     }
 
     }
  }
+
  }</lsl>
  
 
By the way, the results I got from this test shows that it takes 3486 bytes of memory to
 
By the way, the results I got from this test shows that it takes 3486 bytes of memory to
 
append 100 copies of "hello" to a global list when compiled with LSL, and 752
 
append 100 copies of "hello" to a global list when compiled with LSL, and 752
 
bytes when compiled with Mono. These results are a little different than those given in
 
bytes when compiled with Mono. These results are a little different than those given in
[[http://wiki.secondlife.com/wiki/LSL_Script_Memory this page]], and that's why it can
+
[[LSL_Script_Memory|this page]], and that's why it can
 
be illuminating to make these measurements in your own scripts.
 
be illuminating to make these measurements in your own scripts.

Revision as of 19:07, 12 December 2009

Measuring Script Memory Usage

In addition to upcoming new tools that will let us conveniently measure scripts in use, scripters can also use llGetFreeMemory() to make more detailed measurements of how specific functions or statements affect memory usage.

The memory a script uses is:

  • Starting memory (65536 bytes for Mono, 16384 for LSL)
  • minus program statements (e.g., a = 2;)
  • minus static (pre-defined) variables (e.g., integer a;)
  • minus dynamic (created when running) data and overhead (e.g., f("a" + "b") )

Here is an LSL statement that reports how much free memory you have at the moment it's called:

<lsl>llOwnerSay((string)llGetFreeMemory() + " bytes free");</lsl>

To report how much memory is used (instead of free) in a Mono script, subtract free from total memory:

<lsl>llOwnerSay((string)(65536 - llGetFreeMemory()) + " bytes used");</lsl>

For LSL-compiled scripts, change 65536 to 16384.

It makes a difference where you place llGetFreeMemory() in your script. To see how much memory is used by the script's program code and static (pre-defined) variables, put llGetFreeMemory() as the first line in state_entry() or on_rez() in the default state. To see how much memory your script used including all runtime dynamic data, run llGetFreeMemory() as the last statement executed after your script has created all the data it's going to.

To see how much memory is used by a one or a few program statements, measure the memory before and after and take the difference. Be aware that your instrumentation for measuring memory might itself consume some memory if it uses variables or function calls, so do all that prior to the measurement. Also be aware that the way the LSL and Mono back ends allocate memory (and perhaps from fluctuations due to garbage collection), you might get varying results from individual measurements, so it's sometimes useful to take an average.

For example, suppose we want to measure how much memory gets consumed each time your script saves a short string to a global list. Here is the statement that we want to measure:

<lsl>aList += "hello"; // appends one 5-char string to the list</lsl>

To measure the memory used by this statement, we'll average the memory consumption over a hundred loops. We'll also globally declare the temporary variables we'll need for the measurement so that between the start and end of the test, there will be no extra variables allocated or function calls made except the code being measured:

<lsl>

list aList;
integer startFreeMem;
integer endFreeMem;

default
{
    touch_start(integer num)
    {
       integer loops = 100;
       startFreeMem = llGetFreeMemory();

       while (--loops >= 0) {
           aList += "hello";  // appends one 5-char string to the list
       }

       endFreeMem = llGetFreeMemory();
       llOwnerSay("Change in free mem: " + (string)(startFreeMem - endFreeMem));
    }
}</lsl>

By the way, the results I got from this test shows that it takes 3486 bytes of memory to append 100 copies of "hello" to a global list when compiled with LSL, and 752 bytes when compiled with Mono. These results are a little different than those given in this page, and that's why it can be illuminating to make these measurements in your own scripts.