Memory Usage Optimizing, static and new
I've already searched the forums, here http://forums.alliedmods.net/showthread.php?t=107303 and here http://forums.alliedmods.net/showthread.php?t=106358
but i didn't understand exactly what i am looking for. I made a function to the AMX Match Deluxe so that the plugin can set the players nick like so a.Nick or b.Nick for Team A or Team B, similar to the Gather Network concept, checked once per round, at the spawn. And .. after a small delay, i start the for loop. What is recommended here ? static or new ? Ex: PHP Code:
PHP Code:
And in conclusion, from what i understand from the 2 topic posted in the beggining, the static function are overwritten over and over again. Then if i replace in my sample "static name" with "new name" the memory usage wouldn't be the same ? if not, what's the difference between static and new in mu sample above. Thank you. |
Re: Memory Usage Optimizing, static and new
New allocates memory for new variable, sets allocated memory to 0's and then, after function is over, frees memory (destroyes variable). It does so every time function is called.
Static allocates memory just once and keeps variable. If your function is used often or you allocate big amount of data (and function is used more than at least once) its wiser to use static. If your function isn't used often it's better to use new, so memory used by variables from your function, when function done executing will be freed. I don't know about pawn, but using new in extremly oftenly called functions will cause memory fragmentation in some languages, not to mention perfomance reduction. On other hand, the more static you use, the more memory your plugin will use for sure. I prefer to go with static in most cases. Here is small example. Like in regular functions, variables declared in for(){} statement are destroyed after statement done executing: PHP Code:
|
Re: Memory Usage Optimizing, static and new
now i understand, thank you very much for your time.
LE: If i want to use a for loop every round, that affects all connected players, it is recommended to use static or new ? Thank you. |
Re: Memory Usage Optimizing, static and new
Quote:
You can test two methods with amxmodx profiler to see the differance. http://forums.alliedmods.net/showpos...8&postcount=51 http://forums.alliedmods.net/showpos...3&postcount=37 P.S. Don't forget that static variables are not auto-reseted to 0 every time function is called, you have to do it manually. I got stuck with unknown error here: PHP Code:
|
Re: Memory Usage Optimizing, static and new
i understand, so in conclusion, if you use a variable very often, it is recommended to use static, something similar to a buffer for example.
If you use a variable rarely it is recommended to use new, so that the plugin can alocate the necesary memory only then, and destroy's it when isn't necessary anymore. Thank you :D |
Re: Memory Usage Optimizing, static and new
DjOptimuS
You got it right. Also i added another small example about few things you should keep in mind about static (see my previous post). |
Re: Memory Usage Optimizing, static and new
the profiler is a great invention. analyses everything.
Thank you for the sample. |
| All times are GMT -4. The time now is 04:27. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.