• Post Reply Bookmark Topic Watch Topic
  • New Topic

What do you mean by memory leak in Java?  RSS feed

 
pawan chopra
Ranch Hand
Posts: 419
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one explain with example that what is memory leak in Java? If there is any other thread on this topic , that will be also good. Thanks!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A memory leak in general means that memory blocks are not released when they are no longer needed. In Java terms it means objects are not garbage collected.

Memory leaks in Java are much rarer than in C or C++ where you have to clean up your own objects, but it's still possible by keeping references. A very simple example:

Since all instances will always have at least one reference (in the List), no Test object will ever be garbage collected. Therefore, your memory usage will only increase with each Test object you create; they all remain in the memory for as long as the JVM is running.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob Prime,

sorry ! , i can not get your example. if possible can you elaborate the example please
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See what is memory leak?
You know we've some fixed memory which needs to be released by unused objects so as to facilitate usage by other objects.(Garbage collection)
If the memory is not released then it will result in memory leak.
So if Garbage collection is not done then it will result in memory leak.

So now you need to understand when an object will not be garbage collected.
When an object atleast has one reference on it, then it won't be garbage collected.

Here t references the Test object & is a reference.

In the Rob prime's example all the objects are placed in a list & they are referenced simulataneously.
So no possibility of garbage collection so memory leak is possible.
Got it seetharaman venkatasamy!
Regards
 
Mustafa Musaji
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What he is saying is if you create an instance of Test it will always be referenced by the list and therefore never be garbage collected even if where you created the instance is not referencing that object anymore.



Hope that clears things up.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's say an instance of the Test class defined above get's declared and initialized inside a method; let's call this method foo().
Also, let's say the foo() method will not let a reference to this new instance of Test escape its method local scope.
In this scenario, by the time the foo() method completes the Test instance should be eligable for garbage collection, right?
Wrong. Although it appears, on the surface, that only the foo() method retained a reference to the Test instance, another reference actually does exist.
The Test class itself maintains a static List filled with references to every Test instance ever created.
Because every Test instance will always have an active reference pointing to it, no Test instance will ever be eligable for garbage collection, including the Test instance declared an initialized within the foo() method.

Edit: Too slow...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ramya and others

one more thing, you mean that if you give Test test = new Test(); simply ,then the test object wont garbage collect ?

 
pawan chopra
Ranch Hand
Posts: 419
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply!!
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test test = new Test(); simply ,then the test object wont garbage collect ?

No not like that.
If the Test object referenced by test has not been used for quite some time determined by JVM then it may be Garbage Collected.
Correct me if I'm wrong.
Regards
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:
one more thing, you mean that if you give Test test = new Test(); simply ,then the test object wont garbage collect ?


Yes, Test objects (of the class type created by Rob) will not be GC'ed, even if you no longer have a reference to it. The reason is that the Test object saves a reference of itself somewhere, so even if you don't reference it, there is still a reference to it.

Now... as a side note... Java seems to have a somewhat different definition of memory leak than with C/C++. In C. a memory leak is basically caused by lost of memory. For example, a pointer is gotten from a malloc() call. This pointer is either lost, or some pointer arithmetic is done that changes it, and the application can't free it anymore.

In Java, technically, the application can free it, as there is a reference to it somewhere (that can be accessed and null'ed out). The problem is... the developer doesn't know about it, or has forgotten about it. In the case of the Test code, let's say that one developer did it for caching purposes -- and the other developers don't know about it. A few years go by. Developers come and go. The application ballons to millions of lines of code. And there you go... you have an application that grows in memory footprint, as it is being used, and noone knows why.

Henry
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even when the class gets unloaded?

edit: and is that even possible? I was thinking along the lines of removing the reference to the list so the test objects become a collectable island but I can't remember the rules of unloading exactly - will go and figure it out.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all Ranchers
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the Test object referenced by test has not been used for quite some time determined by JVM then it may be Garbage Collected.

Is this possible?
Regards
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramya narayanan wrote:
If the Test object referenced by test has not been used for quite some time determined by JVM then it may be Garbage Collected.

Is this possible?
Regards
That doesn't sound right, no; if there is still a reference then the object isn't garbage collected. In fact there is a rule of thumb that older objects are more likely to be "current" than new objects; lots of new objects have a short lifetime before being discarded, so an object is old because it hasn't been discarded.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!