Memory leak in Java? -
i wondering if following code produced sort of memory leak in java. anyways here's code:
public class test { public static void main(string[] args){ a = new a(); a.x = new a(new a(new a(new a(a)))); a.x.x = null; } } class a{ public x = null; a() {} a(a a){ x = a; } }
if number a's:
a = new a(); // 1 a.x = new a(new a(new a(new a(a)))); // 2 3 4 5
then have circular chain:
a → 1 → 2 → 3 → 4 → 5 → 1
when break circle using a.x.x = null
, get:
a → 1 → 2
instances 3, 4, , 5 eligible garbage collection.
then, when main
exits, a
goes out of scope, , instances 1 , 2 eligible garbage collection.
note program end before gc has chance anything.
Comments
Post a Comment