| 12-17-2006, 01:54 AM | #1 |
From what I know, a memory leak is something created that is stored in memory(to allow faster accessing of it), and if not cleaned(told it is no longer needed), it remains in memory and thus takes up memory until the game is ended. A variable also must be saved in memory, and if not nullified, don't they do the same thing? I am refering to globals and locals. Is there anyway to nullify variables other then the 'leaking' ones? |
| 12-17-2006, 04:36 AM | #3 |
Global Variables always leak, but you can delete its value, so only the identifyer(keyword or name of the variable) persists. and yes if you want to delete a value of an integer that you aren't any longer using, you set its value to 0 and that's it, it will just save you 2 bytes of memory. |
| 12-17-2006, 12:18 PM | #4 |
SO Strings and Integers do not need to be thought about, however all variables containing anything else should be nullified when not in use? I see why the game didn't like it when I tried to set a integer variable to 'null' now. |
| 12-17-2006, 12:45 PM | #5 |
Strings need to be thought about. JASS:function getnondots takes string s returns s local integer i=0 local integer L=StringLength(s) local string r="" local string ch loop exitwhen i==L set ch=SubString(s,i,i+1) if(ch!=".") then set r=r+ch endif set i=i+1 endloop return r endfunction JASS:function getnondots takes string s returns s local integer i=0 local integer k=0 local integer L=StringLength(s) local string r="" local string ch loop exitwhen i==L set ch=SubString(s,i,i+1) if(ch==".") then if(k!=i) then set r=r+SubString(s,k,i) endif set k=i+1 endif set i=i+1 endloop return r endfunction Both functions do the same thing but the second one will create less unnecessary strings. Do you want to see a nice way to leak stuff? JASS:function err takes nothing returns nothing local integer array b ///... loop // .. do stuff (no exitwhen) call TriggerSleepAction(5.0) endloop endfunction Each time you do ExecuteFunc("err") it will leak 32KB |
| 12-17-2006, 01:41 PM | #6 |
Integer and real variables are the data in themselves, rather than pointers. As such, they cannot leak. Setting integers or reals to 0 does not save memory. Strings are pointers, but the objects they point to are never cleaned up, so leaks are permament. As such, the way to avoid these leaks is to avoid creating distinct strings when not needed. Handles are pointers. So long as a variable points to a handle index, the handle index will not be cleaned. However, there is also an oddity that locals that are still pointing at a handle don't decrement the count on the handle index count. So locals must not be left pointing at a non-permament handle when the thread ends. For permament handles, such as players, it makes no difference as the handle index will never be cleaned anyway. All variables (except reals perhaps (?)) are 32 bits, that can be expressed as an integer. |
| 12-17-2006, 07:33 PM | #7 |
Reals too |
| 12-17-2006, 09:55 PM | #8 |
Strings allocate memory on the first run through, but if you use the same string again, it will not leak a new value. Making a trigger that generates a random string every second would cause problems, later on. A constant string that is used periodically will not be a problem at all. |
