my name is GYAN
my name is GYAN

Reputation: 1289

How JVM stores meta information of a class?

We use reflection for a user defined class Employee as:

Employee e = new Employee();
Class c = e.getClass();

As per my knowledge first JVM loads the bytecode of the class Employee, then it also create an object of Class.class for each loaded class (class Employee here). In the object of Class.class JVM stores meta information about the recently loaded class.

Meta information of a class are "name of methods", "name of fields" etc. Class of these types such as "Method", "Field" etc are defined in java.lang.reflect package.

I seen code of Class.java. I found methods in Class.class which are returning objects or array of objects of these types such as "Method", "Field" etc. But there is not a field in Class.class whose type is "Method", "Field" etc.

If my above statements are wrong, please make me correct. If above statements are not wrong then I have following doubts: 1). In which field of Class.class various information about a class get stored? 2). In which memory area of JVM object of Employee and object of Class.class get stored? 3). In which memory area of JVM bytecode of Employee and bytecode of Class.class get stored?

Upvotes: 2

Views: 1227

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533670

In which field of Class.class various information about a class get stored?

This information is store off heap in the PermGen (< Java 7) or MetaSpace (Java 8+) You can't see it from Java directly.

In which memory area of JVM object of Employee and object of Class.class get stored?

All objects are stored on the heap. Smaller objects are created in the Eden space.

In which memory area of JVM bytecode of Employee and bytecode of Class.class get stored?

The byte code is stored in the PermGen/Metaspace, if it is stored at all. In theory, though not in practice, the JVM could re-read the class file as required.

Upvotes: 5

Related Questions