Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does reflection affect Perm size?

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?

like image 941
Suraj Chandran Avatar asked Dec 01 '09 12:12

Suraj Chandran


People also ask

What is Max perm size?

The perm size should be set to 1024 Megabytes. The maximum perm size should be set to 1024 Megabytes. Oracle recommends that -Xmn and -Xmx be set to the same value. This eliminates potentially costly heap reallocations, and can reduce the amount of heap fragmentation that can occur.

What is perm size in Java?

It's is initial value of Permanent Space which is allocated at startup of JVM. It's maximum value of Permanent Space that JVM can allot up to. Examples of using -XX:PermSize VM (JVM) option in java > Example1 of using -XX:PermSize VM (JVM) option in java > java -XX:PermSize=512m MyJavaProgram.

Why does Java 8 remove PermGen space?

The main reason for removing PermGen in Java 8 is: It is very hard to predict the required size of PermGen. It is fixed size at startup, so difficult to tune. Future improvements were limited by PermGen space.


2 Answers

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.

like image 78
Thomas Jung Avatar answered Oct 16 '22 15:10

Thomas Jung


Reflection can generate classes, depending upon implementation. You may, for instance, need to use a reflection artifact thousands of times before it is compiled to bytecode.

As ever, the advice is: Profile your code in realistic situations.

like image 39
Tom Hawtin - tackline Avatar answered Oct 16 '22 16:10

Tom Hawtin - tackline