How can I find objects that are no longer in use?

I am working on a large Java project that has existed for some time (i.e. before my time). The program works in several stages, and many objects are saved from earlier phases and are used at later stages. I found several groups / categories of objects that are saved for no good reason, but I suspect there are more.

So my question is: are there any good tools that can show me which objects were not affected, since a specific point in time did not concern until the end of the program. I used "yourkit" to examine all the objects, but it is often unclear whether they need to be saved or not. If I had something that combined the memory / message calculations of your whales with some kind of coverage tool, I would be a happy person.

+3
source share
2 answers

You can use VisualVM to examine the heap and to monitor garbage collection. You should be able to set a breakpoint in your program, and then take a look at things that you suspect should not be stored and find out what needs to be done with them.

(JProbe?), , , 8-9 , , - , .

+2

, .

  // Create the weak reference. 
     ReferenceQueue rq = new ReferenceQueue(); 
     WeakReference wr = new WeakReference(object, rq); 
  // Wait for all the references to the object. 
  try { 
         while (true) 
        {  
              Reference r = rq.remove(); 
              if (r == wr) 
               { // Object is no longer referenced. } 
         } 
     } 
     catch (InterruptedException e) { } 
+3

Source: https://habr.com/ru/post/1729256/


All Articles