Jump to content
Sign in to follow this  
Wandering Soulstar

Memory Use

Recommended Posts

Hi all,

Am unable to get in world atm to check this, and so thought I'd pop the question out. Looking to undertand which of the following is better (or if there is no difference) from a script memory usage perspective:

for (x = 0; x < num; x++)
{
	integer recordIdx = llList2Integer(fRecords, x);

	//Do some stuff
}

or declaring recordIdx outside the loop:

integer recordIdx;

for (x = 0; x < num; x++)
{
	recordIdx = llList2Integer(fRecords, x);

	//Do some stuff
}

Thanks in advance!!!

Wanda

Share this post


Link to post
Share on other sites

According to this unofficial source, the global declaration would use more memory. I'd nonetheless adise measuring the effect in your script.

[EDIT: Oh, re-reading, I'm not sure you meant the variable to have global scope in either case, and I doubt it matters if not.]

Share this post


Link to post
Share on other sites

For the system , nobody knows . ( so maybe it may make more work for the garbage collector  ?)

For the memory counted by Linden  to know if your script will reach heap collision error , definitevely no.

 

 

 

 

integer memory0;
integer memory1;
integer n;

key f()
{
key result;
n=2000;
memory0 = llGetUsedMemory();
key mykey0 = llGenerateKey();
do
{
mykey0 = llGenerateKey();
} while ( --n );
memory1 = llGetUsedMemory();
llOwnerSay(llList2CSV(["mem f used",memory1-memory0]));
}
key g()
{
// in this function , we declare a key , so 36 chars = 72 bytes
// inside the loop .
// If we had a new allocation at each step , we would get 72 bytes * 2000 steps
// = 144 000 bytes used
// But the script doesn t crash for heap memory .
// So :
// - either the memory is allocated in th system but not counted by Linden
// but it s not counted , the garbage collector could have work on 144000 bytes
// - either the variable inside the loop is reused ( optimization by compiler )

n=2000;
memory0 = llGetUsedMemory();
do
{
key mykey1 = llGenerateKey();
} while ( --n );
memory1 = llGetUsedMemory();
llOwnerSay(llList2CSV(["mem g used",memory1-memory0]));
}
default
{

touch_end(integer total_number)
{
f();
g();
}
}

 

Share this post


Link to post
Share on other sites

Local variables go to the stack. That means you don't need any garbage collection for local variables. Once you leave the scope the stackpointer is set back. So it doesnt matter where you put the variable.

Only question is: does SL initializes a variable when you declare it? Or is it clever and waits for the 1st variable read for initialization? In the 2nd case it doesnt matter if you declare a variable in or outside of a loop. In the 1st case the variable is initialized in every loop. That has no influence on the memory but on the cpu load.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...