How to unset variables and arrays in code (to free memory)?

Sir Isaac

Member
Yet another simple question, but instructions on optimizing B4PPC code are so scarce... Is there a command like 'unset' to explicitly kill a variable/array (say, after splitting or dublicating its data) and thus free the memory? In general, what are good practices for keeping memory print (and CPU load) as low as possible? I know that .Net does garbage collecting and all that jazz, but anyways..?
 
Last edited:

mjcoon

Well-Known Member
Licensed User
Yet another simple question, but instructions on optimizing B4PPC code are so scarce... Is there a command like 'unset' to explicitly kill a variable/array...

Since arrays have to be global they do not go in and out of scope and will retain the size at which they were last used. So if the data is finished with you could set the "Dim" to zero. I do this myself but have no idea if it would make a tangible difference. Must depend on the size (including that of the strings if a string array).

Garbage collection cannot scavenge storage that is potentially still in use!

Mike.
 

Sir Isaac

Member
Did you get any memory problems?
Nope, not me, man, but Billy (Gates) did.
That's why people are crazy about Androids and stuff these days.

Garbage collection cannot scavenge storage that is potentially still in use!
Mike.

That's exactly what I mean. For example, if I StrSplit a really HUGE string (say, an equivalent to hundreds of texts joined together) into arrays, and then again parse those arrays into particular values, how can I tell GC that the first two copies of data is garbage for me? ReDim to (0)? Well, we have to believe it works this way, but who knows..? And how to ReDim the first HUGE variable, since it is not an array?

And remember why did Agraham introduce StringBuilder? The way .Net handles strings in quantities is far from perfect and always tends to unnecessary overpopulation.
 
Last edited:

mjcoon

Well-Known Member
Licensed User
... And how to ReDim the first HUGE variable, since it is not an array?

I like to answer the easy ones:
B4X:
variable = ""

Andrew Graham may point out that it is not really the variable that is being altered, but the separate large string constant (that it used to point to) which is now redundant and can be scavenged.

Mike.
 

Sir Isaac

Member
= "" <> unset, of course, but this is basic, after all)

B4X:
GC.New1
Ahab=FileReadToEnd (MobyDick)
GC.Unset(Ahab)
 

agraham

Expert
Licensed User
Longtime User
And remember why did Agraham introduce StringBuilder? The way .Net handles strings in quantities is far from perfect and always tends to unnecessary overpopulation.
That's not true. there is nothing inefficient about .NET string handling. In fact it is fairly well optimised. In managed laguages like Java and .NET strings are immutable objects for several very good reasons, the main one being that because they they are treated as value objects but are accessed through a reference they must be immutable to behave as expected. Stringbuilder exists in these languages to make dynamically assembling strings more efficient.

If you really want to free a big string then just assign an empty string to any variables referencing it and the garbage collector will see that no references to it exist any more and will GC it. Similarly if you re-Dim an array then all the strings in it that are not referenced elsewhere will become candidates for GC.
 

agraham

Expert
Licensed User
Longtime User
= "" <> unset, of course, but this is basic, after all)
I've no idea what you mean by that, but you are changing the reference in the variable that was pointing to a large string to one that now points to a null (small/non-existent) string so the GC will see that the large string is no longer referenced and will GC it.

However as Erel stated, in general you should normally forget about memory issues and let the GC take care of it. The one exception to this in practice in the Compact Framework is bitmaps because the managed bitmap looks small to the GC because it actually points to a large unmanaged area of memory so multiple bitmaps can cause out of memory errors in unmanaged memory as you can run out of unmanaged memory before the GC sees the need to do a collection which would free both the managed and unmanaged memory.
 

Sir Isaac

Member
I've no idea what you mean by that.
Actually, this Celtic design stands for "I prefer to (have an option to) kill a global variable myself whenever I want rather than to change its reference to empty string and wait for some mysterious garbage brownies to dig-dig-dig my precious data :D". But you are kidding, too, you've got the point and made it clear.

Guys, it's a pleasure to read your comments, they are real quick and informative. There's always a great temptation to ask more and more :D
 
Last edited:
Top