Question

I've got a question about garbage collector and Object.finalize(). I created a class which implements the finalize-method. If I run my app and force GC in DDMS finalize is called on objects of my class with no reference left. But if I call System.gc() within my app finalize is not called. Calling System.exit(0) when leaving the app doesn't cause finalize neither. Why is finalize not called?

Était-ce utile?

La solution

But if I call System.gc() within my app finalize is not called.

That is because your object was not garbage-collected. gc() does not do a complete GC. Instead, it collects some garbage, then returns.

Calling System.exit(0) when leaving the app doesn't cause finalize neither.

That is because your process is being terminated. Finalizers are not run on process termination. And, as Raghunandan noted, please do not call System.exit(0).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top