finalize method()

Hello Everyone,

I m having a very strange problem..for which i cant find what the problem could be..

the problem is that i m running a simple java class in which i have written a finalize method where i m deallocating certain stuff...now as i run the method from the IDE (visual age) it runs perfect..but why try running the same program through command prompt the finalize method never gets called and nothing is even printed and even no exception...the same thing runs perfectly in the ide and produces the desired result.

[557 byte] By [mfarhans] at [2007-9-26 2:36:59]
«« Arrays
»» JRE
# 1

I'm not sure, but I think that what's happening is that garbage collection is never happening in the command line case. Finalizers are run as part of (before? during?) gc. If your program does its work without ever needing to free up additional memory, gc may never happen and the program just exits.

You could try, as the last step of your program, setting all references to null and then calling System.runFinalization(). However, that doesn't necessarily gurantee that finalizers will be run.

On the other hand, when the VM exits, whatever resources youre objects were holding should be freed up anyway, no?

jverd at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...
# 2

What's happening is that your IDE is taking up memory space; which isn't taken up in the command line version, the JVM never runs low on memory, and the garbage collector never runs.

You cannot force the gc. Too many people think it can be done. It cannot. System.gc() doesn't even do it - it's a suggestion to the JVM to gc, it doesn't have to obey.

Same goes for System.runFinalization()

I've never seen a valid reason where finalize() must run. Maybe you have some non-memory resources allocated (sockets? files?) and want them to be released. Then you must track when you need the stuff released and explicitly do it, using a cleanup() method or similar.

I know it's a pain, this is when you want a destructor like C++, but hey, can't win always.

alan.mck at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...
# 3
but dont u think that the garbage collector is called before the program terminates...?
mfarhans at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...
# 4

> but dont u think that the garbage collector is called

> before the program terminates...?

see posts on AdvLangTopics. In answer to this - WHY? IF the VM is exiting, it has no need to free memory, so why would the garbage collection run. I think that the only realistic way to "force" resource-cleanup is to use try{} finally{} in a clever way. If the VM comes down unexpectedly, then there is nothing that can be done.

oxbow_lakes at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...
# 5

Well i have just use the System.gc() command at the very end of the program and it does inititiates the finalize() method everytime the program ends i have tried running it a lot of times and it is executing the finalize method for every execution thought it is mentioned that that system.gc() does not forces garbage collection its just tells that it needs to be garbage collected but when it never knows..but here it is being called every time running the program..giving the strange impression as if the garbage collection does get initiated by it.

mfarhans at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...
# 6
Why do you ask for help (multiple times on different forums, no less), then disagree with all the advice that you get? I get the strange impression that you really do not want help.
schapel at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...
# 7
Is the garpage collector part of java API or it it implemented in JVM's code? It would be intresting to look what it REALLY does?
mikaelkuisma at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...
# 8

If you flip a coin ten times in a row and it comes up heads every time, does that mean that all coins always come up heads when flipped? System.gc() does not force garbage collection. It's a suggestion. Some VMs may be more cooperative/aggressive about it, but you cannot rely on it. About the only use I can see for System.gc() is if you know you're at a point where a lot of memory can be freed up, and it's less disruptive to program flow to do it now than to have the VM decide to do it a little bit later when something important is going on. Again, though, you may or may not be lucky, and it still may not happen until the most inopportune time.

As for gc running when a program exits--No. Again, no guarantees. Certainly a VM could be written such that it does that, but there's really no reason to. As somebody pointed out, when the VM is exiting, it's done with all the memory it's used, so why go through the time and trouble of releasing it, when it's not going to be needed again anyway?

jverd at 2007-6-29 10:05:37 > top of Java-index,Archived Forums,Java Programming...