Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perm space vs Heap space

First, What is the difference between Perm space and Heap space (What and how does the JVM choose to use each memory space)?

Second, but most importantly, what sort of ratio would be recommended for a standard MVC type java application?

like image 515
Gareth Avatar asked Jan 31 '11 07:01

Gareth


People also ask

What is perm space?

The permgen space is the area of heap that holds all the reflective data of the virtual machine itself, such as class and method objects.

Is permanent generation part of heap?

PermGen (Permanent Generation) is a special heap space separated from the main memory heap. The JVM keeps track of loaded class metadata in the PermGen. Additionally, the JVM stores all the static content in this memory section.

Is Metaspace part of heap?

Metaspace is part of Native Memory and NOT part of Java Heap.


2 Answers

The heap stores all of the objects created by your Java program. The heap's contents is monitored by the garbage collector, which frees memory from the heap when you stop using an object (i.e. when there are no more references to the object.

This is in contrast with the stack, which stores primitive types like ints and chars, and are typically local variables and function return values. These are not garbage collected.

The perm space refers to a special part of the heap. See this SO answer for an explanation: What is perm space?

like image 154
Olhovsky Avatar answered Sep 22 '22 00:09

Olhovsky


Personally, I wouldn't consider PermGen a special part of the heap.

I'd much prefer to think of heap as a memory area dedicated to store object instances while PermGen as an area dedicated to store class definitions. As a result, a heap's lifecycle is tied to an application while PermGen's lifecycle is tied to a JVM.

One of the best examples why an application and its JVM can have different lifecycle is in a Java EE container. In an app server, applications can be deployed and undeployed without restarting the server. During the undeployment (or redeployment), it's easy to release all the object instances i.e. heap space, but it's rather tricky to clear all the classes loaded by this app from PermGen because some of the classes can still be referenced by the JVM.

One of such case is the Leaking Drivers. When an app is deployed, a JDBC driver is loaded and registered with the DriverManager. When this app is undeployed, the DriverManager lives on and holds a reference to the driver, its original class loader, and everything this class loader loaded. As a result, a memory leak in PermGen is created, but it's no fault of the application's memory management.

It's true that JVMs like JRocket don't have PermGen at all, everything is stored in heap. Only in such context can you call PermGen a "special part" of heap. Even then, we should still view PermGen and heap differently since they have very different purpose and they have very different types of memory leaks.

Update: In Oracle's JDK 8, PermGen is replaced by "Metaspace" and it is now officially part of the heap. We won't need to specifically tune PermGen any more.

like image 21
Christopher Yang Avatar answered Sep 23 '22 00:09

Christopher Yang