Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java - GC performance hit for inner class vs. static nested class

I just came across a weird effect and while tracking it down, I noticed that there seems to be a substantial performance difference for collecting inner vs. static nested classes. Consider this code fragment:

public class Test {
    private class Pointer {
        long data;
        Pointer next;
    }
    private Pointer first;

    public static void main(String[] args) {
        Test t = null;
        for (int i = 0; i < 500; i++) {
            t = new Test();
            for (int j = 0; j < 1000000; j++) {
                Pointer p = t.new Pointer();
                p.data = i*j;
                p.next = t.first;
                t.first = p;
            }
        }
    }
}

So what the code does is create a linked list using an inner class. The process is repeated 500 times (for testing purposes), discarding the objects used in the last run (which become subject to GC).

When run with a tight memory limit (like 100 MB), this code takes about 20 minutes to execute on my machine. Now, by simply replacing the inner class with a static nested class, I can reduce the runtime to less than 6 minutes. Here are the changes:

    private static class Pointer {

and

                Pointer p = new Pointer();

Now my conclusions from this little experiment are that using inner classes makes it much more difficult for the GC to figure out if the objects can be collected, making static nested classes more than 3x faster in this case.

My question is if this conclusion is correct; if yes what is the reason, and if no why are inner classes so much slower here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would imagine this is due to 2 factors. The first one you already touched upon. The second is using non-static inner classes results in more memory usage. Why you ask? Because non-static inner classes also have access to their containing classes data members and methods, which means you are allocating a Pointer instance that basically extends the superclass. In the case of non-static inner classes you are not extending the containing class. Here is an example of what I'm talking about

Test.java (non-static inner class)

public class Test {
    private Pointer first;

    private class Pointer {
        public Pointer next;
        public Pointer() {
            next = null;
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        Pointer[] p = new Pointer[1000];
        for ( int i = 0; i < p.length; ++i ) {
            p[i] = test.new Pointer();
        }

        while (true) {
            try {Thread.sleep(100);}
            catch(Throwable t) {}
        }
    }
}

Test2.java (static inner class)

public class Test2 {
    private Pointer first;

    private static class Pointer {
        public Pointer next;
        public Pointer() {
            next = null;
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        Pointer[] p = new Pointer[1000];
        for ( int i = 0; i < p.length; ++i ) {
            p[i] = new Pointer();
        }

        while (true) {
            try {Thread.sleep(100);}
            catch(Throwable t) {}
        }
    }
}

When both are run you can see the non-static taking up more heap space than the static. Specifically, the non-static version used 2,279,624 B and the static version used 10,485,760 1,800,000 B.

So, what it comes down to is the non-static inner class uses more memory because it contains a reference (at the very least) to the containing class. The static inner class doesn't contain this reference so the memory is never allocated for it. By setting your heap size so low you were actually thrashing your heap, which resulted in the 3x performance difference.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...