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 741 times)
Memory management
Apr 21st, 2008, 4:45pm
 
Hi everyone,

 I am working in a quite large application and I am very preocupied with the performance.
 My problem is this:
 The program has 4 diferent graphic environments and each one has diferent functionalitys. The user can cycle through them at any time but he can only use one at a time and there is no comunication between these screens.
 Each screen has a class that has methods to draw it and to treat all the events and infomations. so, these are very large classes. I don't whant to keep 4 instnces at the same time in memory at all times when I just need one.
 So, I am thinking of using a global variable with Object data type and use it to keep an instance of the needed class, when the screen changes, I whant to free the memory of that instance and fill the screen variable with other instance of the class that is now needed.

Is there a method for freeing the memory of a variable (like the free() in C)?

And is it possible to give that variable a new value after free'ing it?
Re: Memory management
Reply #1 - Apr 21st, 2008, 5:51pm
 
There is no free() in Java. If all the data goes out of scope, it is automatically freed. You can give extra hints to Java that some data is no longer needing by setting the objects to null.
Re: Memory management
Reply #2 - Apr 22nd, 2008, 12:19pm
 
 Thank you, I found out that processing inherits the Java Garbage collector, so in Java style, I just need to reassign the Object variable and then I could call the Runtime.gc() to tell the garbage collector that it should run as soon as possible.
 Sorry for not researching enough before posting, it was a stupid question.
 I made some tests and to see if it worked but I couldn't get the Runtime.gc() to compile, it says that it is not a static function, so I can't all it like that. But I checked javadoc and Runtime.gc() actually IS a static function. Is it any Processing particularity?

Thanks again for the reply.
Re: Memory management
Reply #3 - Apr 22nd, 2008, 1:34pm
 
It's normally best to just let the GC do it's thing as and when it wants rather than forcing it to run. It will clear up unused things when it needs to free up memory.
Re: Memory management
Reply #4 - Apr 24th, 2008, 11:47pm
 
if you want to force garbage collection you have to use:

Runtime.getRuntime().gc();


Re: Memory management
Reply #5 - Apr 28th, 2008, 4:33pm
 
thanks man!! that's really usefull!! Smiley
Page Index Toggle Pages: 1