Jump to content

Memory Use


Wandering Soulstar
 Share

You are about to reply to a thread that has been inactive for 3219 days.

Please take a moment to consider if this thread is worth bumping.

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

Link to comment
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();
}
}

 

Link to comment
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.

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3219 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...