Application Development

The Contents Of The Java Metaspace Region

Cogent Infotech
Blog
Location icon
Dallas, TX

Java employs automatic memory management. The process includes allocating memory, determining when objects are not referenced, and then de-allocating the memory. Java has a garbage collector that automatically manages memory. The garbage collector in the HotSpot Java Virtual Machine (JVM) is responsible for automatically allocating and de-allocating memory so that the programmer can focus on other aspects of programming. 

When we execute a program, the JVM provides various run-time memory areas. The JVM creates several memory areas, while threads create others. However, JVM's allocated memory areas are destroyed when it exits. 

Similarly, memory areas are created when a thread is instantiated and destroyed on its termination. 

JVM's Memory space is divided into memory pools:

JVM Memory area parts are shown below:

 

‍

The heap area is one of a Java virtual machine's most critical memory areas. All Java objects are stored in those memory areas. When the JVM starts, a heap is created. Usually, there are two parts to the heap. 

Young Generation: 

All new objects are allocated in the young generation memory. When this memory is filled, garbage collection is initiated. Garbage collection of such kind is called minor garbage collection.

Old Generation: 

After surviving multiple minor garbage collection rounds, old-generation memory is where long-lived objects are stored. Whenever this memory gets filled, garbage collection is done. Such a garbage collection is called Major Garbage Collection.

PermGen Memory: 

PermGen memory is permanent generation memory. All static content is stored in the PermGen memory section of the java heap, which is separated from the main memory. Apart from that, the JVM also uses PermGen memory to store application metadata. Information used to describe data is called metadata. As with any other part of the memory, garbage collection also occurs here.

PermGen has a Method Area containing the class structure, constructor, and method code. Before Java 7, that also occurs here as part of this memory. In terms of a disadvantage, PermGen has a limited size, which results in an Out Of Memory Error. 

In the 32-bit version of the JVM, PermGen memory is 64 MB in size; in the 64-bit version, it is 82 MB. This meant JVM had to frequently perform garbage collection, a costly operation that changed the size of PermGen memory. We can also change PermGen memory manually in Java. There is no way to increase the PermGen space automatically, so tuning it is challenging. Moreover, the garbage collector does not sufficiently clean the memory. 

Because of the above issues, Java 8 completely removed PermGen. Rather than PermGen, Meta Space was introduced instead. When the metaspace size reaches its maximum, garbage collection is automatically triggered. 

Metaspace:

Metaspace is a new space to store class metadata. It is not contiguous with the Java heap. Native memory is used to allocate Metaspace. It is the available system memory providing the maximum space to the Metaspace. The maximum space is, however, limited by the MaxMetaspaceSize JVM option.

Code Caches:

The Code Cache Sweeper manages the compilation. During the compilation process, code caches store compiles code generated by Just-in-Time compilers. 

Native Memory:

Native memory refers to the memory that the JVM uses while running on the operating system. It uses the Java heap memory, a JVM-managed subset of the native memory used by the rest of the Java VM, and the code we've written to run on the VM.

Conclusion:

As opposed to Metaspace, Pergem has been used previously, but due to its disadvantages, Java 8 introduced Metaspace with several advantages over Pergem. The JVM provides various run-time memory areas during program execution. The JVM allocates memory to the Java heap, the classes and metadata space, the code cache, and other native memory.

To read more blogs like this, visit the Cogent Infotech website.

FAQs

How do I monitor Metaspace?

JDK Flight Recorder (JFR) can also be used for metaspace monitoring. However, one can also use the "Garbage Collections" view in JMC and "Metaspace Summary" events in the event viewer.

Where is Metaspace located?

Unlike the class itself, which is a java object living on the Java heap, class metadata are not Java objects. They live in native memory, which is referred to as the Metaspace region.

What is Metaspace size in Java?

The size of metaspace in Java is 10% of (maxSizeOfAllSpaces - 100 MB).

How many garbage collectors are there in Java?

In Java, garbage collection makes memory efficient by removing unreferenced objects from heap memory and releasing space for new ones. There are eight types of garbage collectors in the Java Virtual Machine.

‍

No items found.

COGENT / RESOURCES

Real-World Journeys

Learn about what we do, who our clients are, and how we create future-ready businesses.
No items found.

Download Resource

Enter your email to download your requested file.
Thank you! Your submission has been received! Please click on the button below to download the file.
Download
Oops! Something went wrong while submitting the form. Please enter a valid email.