Heap vs Stack vs Perm Space Heap vs Stack vs Perm Space java java

Heap vs Stack vs Perm Space


Simply

  • Heap space: All live objects are allocated here.
  • Stack space: Stores references to the object for variable in method call or variable instantiation.
  • Perm space: Stores loaded classes information

For example:

Student std = new Student();

after executing the line above memory status will be like this.

  • Heap: stores "new Student()"
  • Stack: stores information about "std"
  • Perm Space: stores information about Student class


Forgive me for adding an answer to such an old question - The current answer is great, but misses a couple of edge cases because of static code and Java 8 updates.

Overview

  • Stack
    • Allocated per Thread
    • Stores local references and primitives
    • This is scoped memory - When a method or thread ends, all their data in the stack is lost
    • Has the fastest access, so a local primitive is faster to use than a local Object
  • Heap
    • All allocated object instances exist here
    • Divided into Generations, with the youngest generation being the first place GC looks
    • Available to all threads so allocations and deallocations should be synchronised
    • This memory can become fragmented (but you do not usually manage this yourself)
  • PermGen
    • Stores loaded class information
    • Stores immutable information (Primatives, interned Strings)
    • Stores static class members

Example Code

public class SimpleVal { //The Class (loaded by a classloader) is in the PermGen    private static final int MAGIC_CONSTANT = 42; //Static fields are stored in PermGen    private static final SimpleVal INSTANCE = new SimpleVal(1); //Static field objects are created in the heap normally, with the reference in the PermGen ('class statics' moved to the heap from Java 7+)    private static SimpleVal previousInstance; //Mutable static fields also have their reference in PermGen so they can easily cause memory leaks    private int value; //Member variables will be part of the heap    public SimpleVal(int realValue) {        value = realValue;        ...    }    public static int subtract(SimpleVal val1, SimpleVal val2) {         ....    }    public int add(SimpleVal other) { //Only one copy of any method (static or not) exists - in PermGen         int sum = value + other.value; //Local values in methods are placed in the Stack memory         return sum;    }}public static void main(String[] args) {    SimpleVal val1 = null;    SimpleVal val2 = new SimpleVal(3); //Both of these variables (references) are stored in the Stack     val1 = new SimpleVal(14); //The actual objects we create and add to the variables are placed in the Heap (app global memory, initially in the Young Gen space and later moved to old generation, unless they are very large they can immediately go old gen)    int prim = val1.add(val2); //primitive value is stored directly in the Stack memory    Integer boxed = new Integer(prim); //but the boxed object will be in the heap (with a reference (variable) in the Stack)    String message = "The output is: "; //In Java 7+ the string is created in the heap, in 6 and below it is created in the PermGen    System.out.println(message + prim);}

Java 8 Note: PermGen space was replaced with what is called Metaspace. This still functions the same but can be resized automaticlly - by default Metaspace auto increases its size in native memory up to a maximum (specified in JVM params), but PermGen always has a fixed maximum size contiguous to the heap memory.

Android Note: From Android 4.0 (from 3.0 in practice) Android should honour the described memory contracts - but on older versions the implementation was broken. The 'Stack' memory in Android-Davlik is in fact register based (instruction sizes and counts vary between the two, but for a developer the functionality remains the same).

Finally, for more info the best answer I've ever seen to this subject on StackOverflow is here