0

I'm having a issue with objects that are kept alive longer after its scope is gone. I know that we can't control when the GC is going to delete those objects, but my problem is that those objects are huge and consume large amount of memory. I created a toy example to show my issue. Please, consider the following code:

public class YetAnotherTest {

    public static void main(String[] args) {
        UniqueClass uniqueClass = new UniqueClass();
        for (int i = 0; i < 5; i++) {
            UniqueClass anotherUniqueClass = new UniqueClass("One more ");
            uniqueClass.concat(anotherUniqueClass);
        }
    }

}

class UniqueClass {
    String s;
     public UniqueClass() {
         this.s = new String();
     }
     public UniqueClass(String s) {
         this.s = s;
     }
    public String getS() {
        return s;
    }
    public void concat(UniqueClass uC) {
        this.s = this.s.concat(uC.getS());
    }
}

I run the above code in Debug Mode in Eclipse. When created, the object anotherUniqueClass has id 20 as shown in the following image:

enter image description here

After the first iteration in the for loop, that is when anotherUniqueClass is out of scope, I use the Eclipse debugger tool to query all alive instances of UniqueClass. Then, we can see 2 objects alive, including the one with id 20, which is our anotherUniqueClass out of scope. This is shown in this image:

enter image description here

In my real program, a machine learning algorithm, the correspondent object for anotherUniqueClass is a huge array consuming lots of memory. That is why I am concerned about it being alive after the for loop scope. Considering that I don't have control over GC, is that any suggestion on how to get rid of that alive object? Also, by curiosity, why is it still alive longer after its scope is gone (that is, why did not GC collect it yet)?

Thanks!

jhonatanoliveira
  • 141
  • 1
  • 11
  • 5
    It's "alive" because the JVM didn't need to reclaim the memory yet. – Dave Newton Nov 30 '15 at 16:34
  • Well, you're right in 1 point ; that the GC works in mysterious ways... you can't tell when the GC will be called. – Mohammed Aouf Zouag Nov 30 '15 at 16:34
  • 2
    Scope has nothing to do with garbage collection. Scope is a feature that applies to source code and the use of names in a program. – Sotirios Delimanolis Nov 30 '15 at 16:35
  • You can hint the GC : http://stackoverflow.com/questions/1481178/how-to-force-garbage-collection-in-java – Mr_Thorynque Nov 30 '15 at 16:46
  • Thank you all for your constructive comments. I wasn't happy with my example, since the for loop only builds one reference for ``UniqueClass``. In my "real" piece of code, the for loop also access members of ``UniqueClass`` and that might be the cause of dangling references that doesn't allow GC to delete the object. If I am right, my question can be reformulated: is that a way to detect why an object is alive? That is, which reference (even if a member's one) is keeping the GC away from that object? Thanks again for any insight on this. – jhonatanoliveira Nov 30 '15 at 19:18

0 Answers0