AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

VarSetCapacity (docs), var:="" not freeing memory

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports
View previous topic :: View next topic  
Author Message
Lexikos



Joined: 17 Oct 2006
Posts: 2472
Location: Australia, Qld

PostPosted: Sat Sep 22, 2007 6:56 am    Post subject: VarSetCapacity (docs), var:="" not freeing memory Reply with quote

Quote:
In addition to its uses described at DllCall, this function can also be used to enhance performance when building a string by means of gradual concatenation. This is because multiple automatic resizings can be avoided when you have some idea of what the string's final length will be. In such a case, RequestedCapacity need not be accurate: if the capacity is too small, performance is still improved and the variable will begin auto-expanding when the capacity has been exhausted. If the capacity is too large, some of the memory is wasted, but only temporarily because all the memory can be freed after the operation by means of VarSetCapacity(Var, 0) or Var := "".
As of 1.0.47.04 (and probably earlier), Var := "" does not free the memory used by a variable.
Code:
Loop, 50
  Text = %Text%1234567890
ListVars  ; shows Text[500 of 550]: ...
Pause
Text := ""
ListVars  ; shows Text[0 of 550]:
Pause
VarSetCapacity(Text, 0)
ListVars  ; shows Text[0 of 0]:
Pause

Edit: It seems only variables of capacity > 4 KB are freed when assigned the empty string. Should this be documented? I suppose not, if it's subject to change.


Last edited by Lexikos on Sat Sep 29, 2007 3:27 am; edited 3 times in total
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6309
Location: Pacific Northwest, US

PostPosted: Sat Sep 22, 2007 3:40 pm    Post subject: Reply with quote

duplicated, but I honestly can't say I've even needed it before, or tested it.
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
Lexikos



Joined: 17 Oct 2006
Posts: 2472
Location: Australia, Qld

PostPosted: Sun Sep 23, 2007 3:06 am    Post subject: Reply with quote

Quote:
duplicated
Do you mean you've reproduced the bug, or I've duplicated a thread? I admit I didn't search before posting, but searching now gets nothing (terms: "free memory", "varsetcapacity", "memory freed".)
Quote:
I honestly can't say I've even needed it before
I usually don't have a use for it, either because the variables aren't holding much, or because I try to keep my temporary variables inside functions. In this case, I had read in a number of (entire) sections from an ini file, and the variable's capacity would be roughly the size of the largest section (~500 bytes.) Sure, I could move the init code into a function, but it doesn't seem worth it. At the moment I'm using VarSetCapacity(var,0).

There have been other instances where I've used var = to (hopefully) free the memory - usually when it doesn't seem worth putting in a function (like a simple-ish hotkey.)
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6309
Location: Pacific Northwest, US

PostPosted: Sun Sep 23, 2007 5:08 am    Post subject: Reply with quote

the former
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10464

PostPosted: Mon Nov 12, 2007 12:18 am    Post subject: Reply with quote

It seems best to document this in at least one place, so I've added it to the VarSetCapacity page: "For performance reasons, the memory of a variable whose capacity is under 4096 is not freed by storing an empty string in it (e.g. Var := ""). However, VarSetCapacity(Var, 0) does free it."

Thanks.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group