Question

It seems to be an established fact that Java programs use a lot more memory than C++ programs do. According to an answer on StackExchange, this is due to the nature of Garbage Collection and the bytecode nature of Java's "binaries". But what role does reflection play in this case?

In Java it is possible to enumerate and call the variables and methods of a class as plain text, that is provided at runtime. For this to be possible there has be some kind of stored metadata that links these String keys with their respective attributes.

Is this metadata stored on the heap, and if so, is there a way to disabled it for classes for which I don't need reflection anyway? (Plain old data)

Was it helpful?

Solution

The way java works is it loads all the classes in a memory area called permgen. Any class that is available is loaded in the permgen area irrespective of the method you use it through reflection or not .

Using reflection

Class.forName("MyClass").newInstance() 

Without reflection

new MyClass()

Both the above statements require that class is loaded in permgen

Is this metadata stored on the heap, and if so, is there a way to disabled it for classes for which I don't need reflection anyway? (Plain old data)

And there is no way to disable it.

But closest you can get is implement a custom class loader to load classes. and when you are done with using a particular class loaded by your custom classloader you can release all references to the class and unload it. I hope it helps. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top