wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java object not reclaimed by the JVM leading to memory leak? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java object not reclaimed by the JVM leading to memory leak?" Watch "Java object not reclaimed by the JVM leading to memory leak?" New topic
Author

Java object not reclaimed by the JVM leading to memory leak?

O. joshi
Greenhorn

Joined: Oct 25, 2011
Posts: 3
Hi,

I was wondering if there is any way apart from throwing exception from finalize() method.... in which we can prevent JVM from collecting an object..( an object which is not referenced by any code...).. which may lead to memory leak...??

Thanks,
Omkar...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

omkarok joshiok wrote:
I was wondering if there is any way apart from throwing exception from finalize() method.... in which we can prevent JVM from collecting an object..( an object which is not referenced by any code...).. which may lead to memory leak...??


A memory leak is simply an object that is referenced by the program, that the programmer forgot about.

You don't need to play with the finalize method to cause them -- you can accomplish the leak simply by putting objects in some collection that is referenced by some static variable somewhere. This example, may sound extremely silly, but you will be surprised by how often it happens.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
O. joshi
Greenhorn

Joined: Oct 25, 2011
Posts: 3
My Question is something like this..... Is there any way in which Object which is not referenced by any thread or live code.... may not get garbage collected?.... In your case.. when an object is added to the collection... collection's internal implementation has a reference to this object... so we can't say that there is no reference to it.....
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

omkarok joshiok wrote:My Question is something like this..... Is there any way in which Object which is not referenced by any thread or live code.... may not get garbage collected?

JVM has an Garbage collection mechanism which has evolved over the years. So its less likely that the objects without any valid references will not be garbage collected.

You might come across a situation where in there are multiple objects involved, each connected to the other (like an island), but no valid references to reach to these from the running program. In these cases as well the objects are GCed.

There have been lot of discussions related to garbage collection in the forum- Java in General and Beginners Forum


Mohamed Sanaulla | My Blog
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It's possible that if there is a lot of unused memory, then the garbage collector might not bother to collected unreferenced objects.

But why this question? If you're a beginner in the Java, you're rather wasting your time looking for ways that things might not work the way that the documentation says they work.
O. joshi
Greenhorn

Joined: Oct 25, 2011
Posts: 3
No... not beginner.... have more than 5 years of exp..and SCJP.. SCWCD... SCJD..SCDJWS...SCMCD..... too ..but today one senior at Nomura asked me this.. and gave some example... which I didn't understand..... and as per him.. there is a way in which object may not get reclaimed... leading to memory leak..... I too argued with him on same lines...... but posted it here because I thought I might not be knowing something which may result into memory leak... If anyone has ever experienced that then please let me know.....because JAVA doc says... garbage collector tries its level best to reclaim it but can't guarantee this.... That line has hinted me that there might be a way for this...
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Objects not being reclaimed although there are no more references to it? That would be a GC problem/bug, something that is beyond your control really. So why worry about that, unless you are trying to improve the GC (which is certainly not a beginners topic)?

I'd say its more useful to think about situations where you think objects are eligible for GC, but they don't get reclaimed because you forgot to clear some reference somewhere.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java object not reclaimed by the JVM leading to memory leak?