Wandering Soulstar Posted June 27, 2015 Share Posted June 27, 2015 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 More sharing options...
Qie Niangao Posted June 27, 2015 Share Posted June 27, 2015 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.] Link to comment Share on other sites More sharing options...
Miranda Umino Posted June 27, 2015 Share Posted June 27, 2015 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 More sharing options...
Nova Convair Posted June 27, 2015 Share Posted June 27, 2015 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 More sharing options...
Wandering Soulstar Posted June 27, 2015 Author Share Posted June 27, 2015 Thaks all! And special thanks to Miranda for the example! Link to comment Share on other sites More sharing options...
Recommended Posts
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