today i noticed a large memory use in one of my programs. After some tests i found out a strange behavior with the LIST. I wrote a small sample to point out the problem:
NewList items.s()
MessageRequester("", "Create") ;memory use is now 2.1 MB
For i = 1 To 5000000
AddElement(items())
items() = "This is line number " + Str(i)
Next
MessageRequester("", "Clear") ;memory use is now 384 MB
ClearList(items())
MessageRequester("", "Cleared") ;memory use is still 233 MB
When reaching the third MessageRequester, i expected the same memory consumption as at the beginning, but there are still 233 MB blocked. Why that?
The debugger was disabled. The code ported into Python 2.7.1 works fine (so it's not Fedora or so).
NewList items.s()
MessageRequester("", "Create") ;memory use is now 1.8 MB
For i = 1 To 5000000
AddElement(items())
items() = "This is line number " + Str(i)
Next
MessageRequester("", "Clear") ;memory use is now 276.6 MB
ClearList(items())
MessageRequester("", "Cleared") ;memory use is still 3.6 MB
FreeList(items()) ;memory use is still 0.3 MB ........YeahhHHHHH :)
I did not mention but i tried already FreeList() with NO effect. The only difference is that the list object itself will be freed. On my system there are still 233 MB used. As you can see in your sample on windows, the main memory consumption must be freed on ClearList().
Never trust programs like the task manager about such things. As long as there is no immediate need for the freed memory, the OS will usually keep it reserved for the program for a possible later allocation. So the number doesn't go down immediately. That doesn't mean that the memory is actually still allocated by the program.
MessageRequester("", "Create") ;memory use is now 2.1 MB
Dim items.s(5000000)
For i = 0 To 5000000-1
items(i) = "This is line number " + Str(i)
Next
MessageRequester("", "Clear") ;memory use is now 269 MB
ReDim items(0)
MessageRequester("", "Cleared") ;memory use is still 231 MB
NewList items.i()
MessageRequester("", "Create") ;memory use is now 2.1 MB
For i = 1 To 5000000
AddElement(items())
items() = i
Next
MessageRequester("", "Clear") ;memory use is now 155 MB
ClearList(items())
MessageRequester("", "Cleared") ;memory use is still 4 MB
Freak is of course right - I don't think the empty List would be 10MB though, that will be something else but not important. I used this to verify the RAM on Windows:
@freak: I am using the system monitor from Linux to control the memory consumption. In all above cases, the display seems to be correct, except when strings are involved.
To check if the memory consumption increases more and more, i put the codeblock in a loop and the result was very unexpected: the 384 MB would never be exceeded. So the guess of freak is probably right - the memory is reserved for the current program and will be reused! Nice to know
NewList items.s()
For j= 1 To 3
MessageRequester("", "Create") ;memory use is now 2.1 MB
For i = 1 To 5000000
AddElement(items())
items() = "This is line number " + Str(i)
Next
MessageRequester("", "Clear") ;memory use is now 384 MB
ClearList(items())
MessageRequester("", "Cleared") ;memory use is still 233 MB
Next