when does java deallocate memory for objects;
um i'm working on a midp application that is very memory consuming. i would like to optimise it and make it just the oposite of what it is. now i would like to know when does java dellocate memory for an object, is it when it can find no more references to that object? or some other time, well i know its not when it can find no more references to that object because i've already tried that.
or to make things simpler is there any explicit way to make java deallocate memory for a specific object, like if i have a thread, which is executing a while loop, and i want java to end the thread and free its memory. is there any way to do this?
[656 byte] By [
mariziona] at [2007-10-2 7:17:31]

You can't and don't need to explicitly free up memory. The garbage collector will do it no later than when it is absolutely necessary, and possibly sooner than that but possibly not. You can't control that in your code, but you can tweak the parameters by providing the right startup options to the VM.
http://www.google.com/search?q=java+garbage+collection+tutorial
http://java.sun.com/docs/hotspot/
jverda at 2007-7-16 20:52:42 >

> or to make things simpler is there any explicit way
> to make java deallocate memory for a specific object,
> like if i have a thread, which is executing a while
> loop, and i want java to end the thread and free its
> memory. is there any way to do this?
No, you have to make sure that the thread stops the loop/execution and terminates.
Kaj
kajbja at 2007-7-16 20:52:42 >

You could add the code -
System.gc() to your code.
This code is only an indication for the Garbage Collector to run. It is not guaranteed that the Garbage collector will run.
Additionally like pointed out earlier, look at the startup parameters to control the which garbage collector is used.
whenever u feel the objects will no longer be used in ur application, dereference them, use obj = null;Threads u have to manage on ur own though, and try'n avoid using while loops in run() method
I happen to have quite some J2ME experience and it's not overly wise to count on extensive garbage collection. The garbage collector in limited device VMs isnt as advanced as it's big brothers. Try to avoid excessive object allocation and reuse instances whenever possible.
As for garbage collection, objects will be garbage collected if they can no longer be reached by any of the active application threads and if the garbage collectors deems it necessary to collect garbage, which will probably be when free heap memory becomes sparse or when there's some idle time in your application.
As remonw says - from experience with MIDP, you simply have to be careful to minimise your object allocation, especially in loops.Lots of OO principles start going out of the window quite rapidly when you need to create highly-performant mobile apps.