• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to detect memory leak ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to detect memory leak ?

Here is a test program. when we set course to null, gc will reclaim its memory, but it will not reclaim the set of student's memory. Obviously it is a memory leak. But how to use a profiler tool to detect it ? I have installed the TPTP Eclipse plugin, but that tool didn't tell this leak.

Any advices ?

Thanks.

 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...but it will not reclaim the set of student's memory. Obviously it is a memory leak


Why do you think there is a memory leak?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:

...but it will not reclaim the set of student's memory. Obviously it is a memory leak


Why do you think there is a memory leak?



Agreed. I don't see a memory leak either.

Henry
 
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Tom Reilly wrote:

...but it will not reclaim the set of student's memory. Obviously it is a memory leak


Why do you think there is a memory leak?



Agreed. I don't see a memory leak either.

Henry



yes there is no memory leak...try to read again and see yourself.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, ALL, thanks for your reply.

A course has lots of students, when course is gone, then all students should be gone, too. (let us assume this is right, please don't argue this). We delete course, set the course to null, then we should set all students inside to null, this way will let GC reclaim course and its students memory.

When we have 100 courses, each of them has 100 students, then we will have 100*100 students, and these students have not released its memory. This is a memory leak. isn't it ?

Thanks.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think Students have to depend of the course. Instead course should be dependent on Students. If there are no students in the course- Its worth removing it. So you might have 1000 Students and allocate them across different courses. So you remove the Student only when the Student wishes to be removed

And if at all you want to remove all the Students from the Set- You can as well use a corresponding method to remove all its elements (There should be one I guess)

[Update: Didnt read this "let us assume this is right, please don't argue this"]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Chen wrote:
A course has lots of students, when course is gone, then all students should be gone, too. (let us assume this is right, please don't argue this). We delete course, set the course to null, then we should set all students inside to null, this way will let GC reclaim course and its students memory.



The GC determines whether an instance should be collected by reachability. Simply, if it is not reachable, it will be collected.

In your example, the course objects still have references to the hashset, which still has tons of internal references, which in turn hash those 100 students. But are those students reachable? If you don't have any other variables still in scope, that can reach those students, then those students are not reachable (since the course instance is no longer reachable), and they will be collected, regardless that they have references pointing to them.

Henry

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:The GC determines whether an instance should be collected by reachability. Simply, if it is not reachable, it will be collected.


Then, how the GC knows if an object is reachable ? Can we write a program to prove it ? If that studentSet is static, then when GC will reclaim its memory ?

Thanks.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Chen wrote:
Then, how the GC knows if an object is reachable ? Can we write a program to prove it ?



Depends on the collector. The new collector is a copy collector. Instances gets moved from one side of the heap to the other, as the GC traverse the objects from the roots. Whatever is left over is not reachable.

The tenured collector is a mark and sweep collector. Instances get marked as the GC traverse the objects from the roots. Whatever is left over will be collected during the sweep phase. The sweep process also compacts the heap.

Edward Chen wrote:
If that studentSet is static, then when GC will reclaim its memory ?



The GC will not collect static variables, because static variables are still reachable without using an instance -- it can be accessed by ClassName.staticVar. Or in other words, loaded classes are roots, which can reach static variables.

And BTW, the other roots are all the local variables that are in scope, from all the running threads; And from internal JVM variables, such as the string pool.

Henry
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Depends on the collector. The new collector is a copy collector. Instances gets moved from one side of the heap to the other, as the GC traverse the objects from the roots. Whatever is left over is not reachable.

The tenured collector is a mark and sweep collector. Instances get marked as the GC traverse the objects from the roots. Whatever is left over will be collected during the sweep phase. The sweep process also compacts the heap.



Not fully understand this. For copy collector, do you mean, copy all reachable objects to one side of the heap ? but it will cost a lot when moving an object .

For mark and sweep collector, how sweep process handles memory fragmentation ?

Please give me more details.

Thanks.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Chen wrote:
Not fully understand this. For copy collector, do you mean, copy all reachable objects to one side of the heap ? but it will cost a lot when moving an object .



A copy collector splits the heap in half and move objects between the two halves during the GC cycle. And yes, "it will cost a lot".

The theory behind using a copy collector for the new generation objects, is that most objects do not last very long. Because of this, more object are unreachable than objects that are reachable, and hence, more object do *not* have to be moved. And if an object stays around too long, it will be promoted to the tenured heap, and under the care of the mark and sweep collector.

Edward Chen wrote:
For mark and sweep collector, how sweep process handles memory fragmentation ?



Compaction is done by moving objects.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BTW, this topic has definitely move beyond the "beginner" status. Moving to the general java forum.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edward Chen,
Your post was moved to a new topic.


This topic should be for the further discussion related to the application memory leak.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic