0
0
Javaprogramming~15 mins

Heap memory in Java

Choose your learning style8 modes available
menu_bookIntroduction

Heap memory is where Java stores objects and data created during a program's run. It helps manage memory so your program can use space efficiently.

When you create new objects using the <code>new</code> keyword.
When you want to store data that needs to live beyond a single method call.
When your program needs to manage memory dynamically as it runs.
When you want to understand how Java manages memory to avoid errors like memory leaks.
regular_expressionSyntax
Java
public class Example {
    public static void main(String[] args) {
        // Creating an object stored in heap memory
        MyObject myObject = new MyObject();
    }
}

class MyObject {
    int value;
    // Object fields and methods
}

Objects created with new are stored in heap memory.

Primitive variables like int inside methods are stored in stack memory, but objects live in heap.

emoji_objectsExamples
line_end_arrow_notchThis creates a new object in heap memory and obj holds a reference to it.
Java
MyObject obj = new MyObject();
line_end_arrow_notchThis stores a primitive value on the stack, not in heap memory.
Java
int number = 5;
line_end_arrow_notchHere, obj points to nothing, so no heap memory is allocated.
Java
MyObject obj = null; // No object created, no heap memory used
code_blocksSample Program

This program creates two Person objects stored in heap memory. It shows how references point to objects and how setting a reference to null means the object can be cleaned up by Java's garbage collector.

Java
public class HeapMemoryDemo {
    public static void main(String[] args) {
        System.out.println("Start of program");

        // Create first object in heap
        Person personOne = new Person("Alice", 30);
        System.out.println("Person One: " + personOne);

        // Create second object in heap
        Person personTwo = new Person("Bob", 25);
        System.out.println("Person Two: " + personTwo);

        // Nullify reference to personOne
        personOne = null;
        System.out.println("Person One reference set to null");

        System.out.println("End of program");
    }
}

class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return name + ", age " + age;
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Time complexity: Creating objects in heap is generally fast but depends on JVM implementation.

line_end_arrow_notch

Space complexity: Heap size limits how many objects you can create; too many objects can cause OutOfMemoryError.

line_end_arrow_notch

Common mistake: Confusing stack and heap memory. Variables hold references on stack, but objects live in heap.

line_end_arrow_notch

Use heap memory for objects that need to live beyond method calls. Use stack for temporary primitive variables.

list_alt_checkSummary

Heap memory stores all objects created with new in Java.

References to objects live on the stack, but the actual objects live in heap.

Java automatically manages heap memory with garbage collection to free unused objects.