We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › memory management
Page Index Toggle Pages: 1
memory management (Read 1109 times)
memory management
Jul 6th, 2005, 3:34am
 
I've written a sketch which creates a lot of objects in order to draw a stationary frame. Once the drawing is finished, I'd like to dispose of them and free up some memory. How does this work in Java? Can objects have destructors in addition to constructors?
Re: memory management
Reply #1 - Jul 6th, 2005, 10:57am
 
You shouldn't have to worry about it.  Are you getting OutOfMemory exceptions?

You might have some luck explicitly setting the objects to null before asking for new ones.  But really, Java should take care of it for itself - that's one reason you're using Java!

There isn't an equivalent of a destructor for Java.  There's a dispose function, but as I understand it it's not guaranteed to be called (not least because objects aren't guaranteed to be deleted).
Re: memory management
Reply #2 - Jul 6th, 2005, 1:12pm
 
tom's right, you really shouldn't have to worry about it. but sometimes you might end up in a situation when you'll have to help out the garbage collector (GC) a bit and tell it about a significant amount of just released objects. the reason why this is helpful is that normally the GC runs in its own low-priority thread in the background. if you release a lot of memory hungry objects and then quickly need the space again for new ones, you can trigger the GC directly via:

Code:
System.gc(); 


once the method returns, as much memory as possible should be freed up. using this method of course only makes sense if you delete all object references by setting them to null beforehand... Wink
Page Index Toggle Pages: 1