How does reflection affect Perm size?

java, memory-management, reflection

Solution

The perm space will grow when you execute code that will load new classes or internalize strings. The reflection classes have to be loaded that's for sure. I'm not sure if the reflection API does use internalized strings heavily but it should not be to hard to find out.

For example for the method `getDeclaredMethod(String name, Class<?>... parameterTypes)` the name parameter will be internalized.

This snippet will nicely fills up the perm space:

Random random = new Random();
while(true){
    try {
        X.class.getDeclaredMethod(toBinaryString(random.nextLong()));
    } catch (Exception e) {
    }
}

alt text http://img4.imageshack.us/img4/9725/permgen.jpg

In a real world application it will make no difference.

Problem

I have the understanding that perm size is used to store meta data, that would include byte code, static stuff etc. My question how does usage of reflection affect the perm size, if it ever does. I mean if Program-A uses normal way of running objects and Program-B uses reflection all over, how will the perm-sizes of the two programs compare?

Original source