Raised This Month: $ Target: $400
 0% 

Memory Usage Optimizing, static and new


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DjOptimuS
Senior Member
Join Date: Jan 2009
Old 05-07-2011 , 16:44   Memory Usage Optimizing, static and new
Reply With Quote #1

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:
public CheckNames()
{
    static 
name[32];
    for(new 
i=1;i<=12;i++)
    {
        
get_user_name(iname31);
        
format(name31"a.%"name);
        and 
so onetc.
    }

Also, can i use static instead of
PHP Code:
new CsTeams:team
?

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.
DjOptimuS is offline
SonicSonedit
Veteran Member
Join Date: Nov 2008
Location: Silent Hill
Old 05-07-2011 , 17:04   Re: Memory Usage Optimizing, static and new
Reply With Quote #2

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:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"


public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
// Add your code here...
    
    
for (new i=0;i<100;i++)
        
server_print("\n%d",i)
        
    
server_print("%d",i// You will get Error: Undefined symbol "i" on line 17, because "i" is already freed (destroyed)

The only difference between for(){} and regular functions is that you can't use static in it. Same goes for while (){} statement.
__________________


Last edited by SonicSonedit; 05-07-2011 at 17:14.
SonicSonedit is offline
DjOptimuS
Senior Member
Join Date: Jan 2009
Old 05-07-2011 , 17:09   Re: Memory Usage Optimizing, static and new
Reply With Quote #3

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.
DjOptimuS is offline
SonicSonedit
Veteran Member
Join Date: Nov 2008
Location: Silent Hill
Old 05-07-2011 , 17:16   Re: Memory Usage Optimizing, static and new
Reply With Quote #4

Quote:
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.
There will be no much difference, so in your case it's more about personal opinion/preference.
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:
public clcmd_zp_cs_rebuy(id)
{        
    static 
weapons[32], numiweaponidalready_have_primary_weaponalready_have_secondary_weapon
    
    
if (!is_user_connected(id)||!is_user_alive(id)||is_user_bot(id))
        return 
PLUGIN_HANDLED

    already_have_primary_weapon
=0
    already_have_secondary_weapon
=0
    get_user_weapons
(idweaponsnum)
    
    for (
i=0numi++)
    {
        
weaponid=weapons[i]
        
        if ((
1<<weaponid) & PRIMARY_WEAPONS_BIT_SUM)
            
already_have_primary_weapon=1
            
        
if ((1<<weaponid) & SECONDARY_WEAPONS_BIT_SUM)
            
already_have_secondary_weapon=1
    

After some time i realised that get_user_weapons doesn't SETS num (from get_user_weapons(id, weapons, num)) to the count of weapons were found, but ADDS. And therefore i always had already_have_blah_weapon 1, because in for (i=0; i < num; i++) it was checked that i have, like, 53453 weapons.
__________________


Last edited by SonicSonedit; 05-07-2011 at 17:35.
SonicSonedit is offline
DjOptimuS
Senior Member
Join Date: Jan 2009
Old 05-07-2011 , 17:16   Re: Memory Usage Optimizing, static and new
Reply With Quote #5

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
DjOptimuS is offline
SonicSonedit
Veteran Member
Join Date: Nov 2008
Location: Silent Hill
Old 05-07-2011 , 17:30   Re: Memory Usage Optimizing, static and new
Reply With Quote #6

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).
__________________

SonicSonedit is offline
DjOptimuS
Senior Member
Join Date: Jan 2009
Old 05-07-2011 , 18:10   Re: Memory Usage Optimizing, static and new
Reply With Quote #7

the profiler is a great invention. analyses everything.

Thank you for the sample.
DjOptimuS is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:27.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode