Does declaring many identical anonymous classes waste memory in java?

Posted by depsypher on Stack Overflow See other posts from Stack Overflow or by depsypher
Published on 2011-01-11T02:12:23Z Indexed on 2011/01/11 2:53 UTC
Read the original article Hit count: 168

Filed under:

I recently ran across the following snippet in an existing codebase I'm working on and added the comment you see there. I know this particular piece of code can be rewritten to be cleaner, but I just wonder if my analysis is correct.

Will java create a new class declaration and store it in perm gen space for every call of this method, or will it know to reuse an 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("");
        }
    };
}

© Stack Overflow or respective owner

Related posts about java