Is declaring many of the same anonymous classes a waste of memory in Java?

I recently browsed the following code snippet in the current code base and added the comments you saw there I know this code can be rewritten to be cleaner, but I just want to know if my analysis is correct

Will Java create a new class declaration and store it in the perm Gen space of each call to the method, or will it know to reuse the existing declaration?

protected List<Object> extractParams(HibernateObjectColumn column,String stringVal) {
    // FIXME: Could be creating a *lot* of anonymous classes which wastes perm-gen space right?
    return new ArrayList<Object>() {
        {
            add("");
        }
    };
}

Solution

This class is compiled only once (at compile time) The compiler extracts a class (named myouterclass $1) and uses it Of course, it will create multiple instances, but they will all be the same class You can see that when you compile Java file and view the generated Class file, the internal anonymous class will have a

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>