Proper use of the .New methods, out of memory error

kolbe

Active Member
Licensed User
Hello all,

I have a program that uses a few bitmap objects. I see that the program keeps on allocating memory as it runs. When I change the bitmaps I call the New methods again.

So the question is do the new methods allocate new object memory each time or does it just reinitialize the object and it's memory. I've been under the impression that it just reinitializes the object. Should I be just changing the .value method and avoid calling New over and over. This goes for all objects actually.

Thanks
 

Erel

Administrator
Staff member
Licensed User
The .Net garbage collector should free unused memory.
Do you get an out of memory exception?
It is possible that the bitmaps are still referenced somewhere in your program and therefore their resources will not be freed.
In that case it is better to use Value instead of New which creates a new object.

You could also manually dispose the object before creating a new one:
B4X:
Bitmap1.Dispose
AddObject("Bitmap1","Bitmap")
Bitmap1.New1(...)
 

kolbe

Active Member
Licensed User
Hi Erel,

Yes eventually I get a out of memory exception. I can see that the memory allocated by the program steadily increases. The gccollect method helps but the allocated memory still increases.

I have three bitmaps objects that I continually use in the program. The bitmap continually get changed. At the moment I call new each time I need to change the bitmaps. The bitmap is "on" the screen when new is called. I don't create any additional bitmap objects just reuse the three I already have.

So, if I follow, because the bitmaps are being used/referenced in the program a dispose and addobject is needed to free all memory.

Is this advice for all other objects as well?

Thanks
 

Erel

Administrator
Staff member
Licensed User
GCCollect should not be used. The garbage collector will free unused memory from time to time or when free memory drops below a certain value.

Did you try to assign the Value property instead of calling New?
 

kolbe

Active Member
Licensed User
In some places I load the image from a file. To use the value method it seems to me that I need to then make a imagelist but I don't particularly care to do this because the images are background images and can be many. I can try just to see the result.
 

Erel

Administrator
Staff member
Licensed User
The Dispose / AddObject is a good solution.
Is this advice for all other objects as well?
You should not use it with most objects that unless you repeatedly create and destroy such objects.
 
Top