Javatpoint Logo
Javatpoint Logo

Class Memory in Java

Structure of Class in Memory

Every single class of a Java program gets converted into bytecode when the Java program gets compiled. The main purpose of bytecode is to store the instructions that will be executed by the Java Virtual Machine (JVM). The Class object is responsible for containing all the information and data regarding structure of class, methods, fields, and several metadata.

Class Loading

A dynamic class loading mechanism is followed by Java. The JVM loads all the classes that are required and utilized during the runtime of a Java program. All the class files are found and loaded by the subsystem of class loader. Various class loaders are available such as Bootstrap class loader, Extension class loader, etc. All types of class loaders work collectively for loading classes in the memory allocated to the Java Virtual Machine (JVM).

Let's understand in detail about class memory in Java with the help of few Java example programs.

Example 1:

Filename: ClassMemory.java

Output:

Memory Usage Before Creating Objects:
Total Memory: 123731968 bytes
Free Memory: 121716432 bytes
Used Memory: 2015536 bytes
------------------------
Memory Usage After Creating Objects:
Total Memory: 123731968 bytes
Free Memory: 121150408 bytes
Used Memory: 2581560 bytes
------------------------
Memory Usage After Performing Additional Work:
Total Memory: 123731968 bytes
Free Memory: 118347160 bytes
Used Memory: 5384808 bytes

Let's see another example for the same.

Filename: ClassMemoryDemo.java

Output:

Memory Usage Before Creating Objects:
Total Memory Allocated: 123731968 bytes
Free Memory Allocated: 121716432 bytes
Used Memory Allocated: 2015536 bytes
------------------------
Memory Usage After Creating Objects:
Total Memory Allocated: 123731968 bytes
Free Memory Allocated: 121150400 bytes
Used Memory Allocated: 2581568 bytes
------------------------
Memory Usage After Performing Additional Work:
Total Memory Allocated: 123731968 bytes
Free Memory Allocated: 117522224 bytes
Used Memory Allocated: 6209744 bytes
------------------------






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA