How are Java Objects Stored in Memory?

Last Updated : 1 Dec, 2026

Java manages object memory automatically through the Java Virtual Machine (JVM). Understanding how objects and references are stored helps explain memory allocation, method execution, and garbage collection.

  • Objects and arrays are allocated from heap memory.
  • Local variables, including local object references, are associated with method stack frames.

Example: Creating an object and accessing its instance fields.

Java
class Student{
    String name;
    int age;
    Student(String name, int age){
        this.name = name;
        this.age = age;
    }
}
public class Geeks{
    public static void main(String[] args){

        // Create a Student object
        Student s = new Student("Bob", 20);
        System.out.println(s.name);
        System.out.println(s.age);
    }
}

Output
Bob
20

Explanation:

  • new Student("Bob", 20) creates a Student object.
  • s is a local reference variable associated with the main() method's stack frame.
  • The instance fields name and age belong to the Student object.
  • The name field holds a reference to a String object, while age stores a primitive integer value.

Memory Areas Used for Object Storage

The JVM uses different runtime memory areas to manage objects, references, method execution, and class information.

1. Heap Memory

Heap memory is the runtime memory area used to allocate objects and arrays.

  • Shared among all threads.
  • Stores objects containing their instance fields.
  • Managed automatically through garbage collection.

Scanner sc = new Scanner(System.in);

The new expression creates a Scanner object, and sc, when declared inside a method, is a local reference variable that refers to it.

2. Stack Memory

Stack memory contains stack frames created during method execution. Each thread has its own JVM stack.

  • Stores local variables, method parameters, and intermediate computation data within stack frames.
  • Removes stack frames automatically when their corresponding methods complete.

Example: Understanding local variables and method stack frames.

Java
public class Geeks{
    public static int calculate(int n){
        int ans = n * 10;
        return ans;
    }
    public static void main(String[] args){
        int n = 10;
        int result = calculate(n);
        System.out.println(result);
    }
}

Output
100

Explanation:

  • n and result belong to the main() method's stack frame.
  • The parameter n and local variable ans belong to the calculate() method's stack frame.
  • When calculate() completes, its stack frame is removed and the returned value is assigned to result.

3. Metaspace

Metaspace is a memory area used by the HotSpot JVM to store class metadata.

  • Uses native memory rather than the Java heap.
  • Replaced Permanent Generation (PermGen) starting with Java 8.

Metaspace is a HotSpot implementation detail and is not a separate runtime data area defined by the JVM specification.

Java Object Storage in Memory

When an object is created using the new keyword, the JVM allocates memory for the object and invokes its constructor to initialize it.

frame_4215
Storage of Java Objects in Memory

Explanation: When new Student("Bob", 20) executes, the JVM allocates memory for a Student object on the heap. The constructor initializes its instance fields. The local reference variable s in the main() method's stack frame refers to this object.

Note: The name field stores a reference to a String object containing "Bob", while age stores the primitive value 20. The diagram displays "Bob" directly for simplicity.

Memory Layout of a Java Object

At the implementation level, a Java object's memory layout typically consists of an object header, instance data, and optional padding.

1. Object Header

Contains JVM-specific information used for managing the object, which may include:

  • Class or type information.
  • Locking, identity hash code, and garbage collection information.

2. Instance Data

Stores the values of instance fields declared by the object's class and inherited from its superclasses. Primitive fields contain primitive values, while reference fields contain object references.

3. Padding

Contains unused bytes that may be added to satisfy memory alignment requirements.

  • Does not store application data.
  • Its size depends on field layout, JVM implementation, and configuration.

Note: The JVM specification does not mandate a particular object memory layout or header size.

Garbage Collection and Object Lifecycle

The garbage collector automatically reclaims heap memory occupied by objects that are no longer reachable.

Student S = new Student("Bob", 20);
S = null;

Assigning null removes the reference held by s. If no other references keep the Student object reachable, it becomes eligible for garbage collection.

Garbage collection is not guaranteed to occur immediately. Objects that remain reachable can continue occupying memory even when the application no longer needs them.

Comment