Perm space vs Heap space

JavaPerformanceMemory Management

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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: https://stackoverflow.com/questions/1279449/what-is-perm-space

Solution 2 - Java

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.

Solution 3 - Java

You can NOT give names to allocated memory in the heap.

That means int x (its name) is allocated in the stack. You can reach the pointer by its name, so the pointer is in the stack. You can't reach the object by its name, cause it has no name. Accees to the (nameless) object must be by its pointer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGarethView Question on Stackoverflow
Solution 1 - JavaOlhovskyView Answer on Stackoverflow
Solution 2 - JavaChristopher YangView Answer on Stackoverflow
Solution 3 - JavaPete Del FinaView Answer on Stackoverflow