Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
O. joshi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
O. joshi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic