Movatterモバイル変換


[0]ホーム

URL:


Open In App

Java memory management is the process by which the Java Virtual Machine (JVM) automatically handles the allocation and deallocation of memory. It uses a garbage collector to reclaim memory by removing unused objects, eliminating the need for manual memory management

JVM Memory Structure

JVM defines various runtime data areas used during the execution of a program. Some of the areas are created by the JVM, whereas some are created by the threads that are used in a program. However, the memory area created by the JVM is destroyed only when the JVM exits. The data areas of a thread are created during instantiation and destroyed when the thread exits. These areas include:

Java Virtual Machine (JVM) Memory Areas

The image below demonstrates the Java Memory Area parts:

Java-Memory-Area-Parts
jvm-memory

1. Heap Area

Scanner sc = new Scanner(System.in)

Here, the Scanner object is stored in the heap and the reference sc is stored in the stack

Note:Garbage collection in heap area is mandatory.

2. Method Area

Note:Method area is logically a part of heap, many JVM like HotSpot uses a separate space known as Metaspace outside the heap to store it.

Example: Java Program to demonstrate how java variables are stored in the different memory areas

Java
importjava.io.*;classGeeks{// static variables are stored in the Method Areastaticintv=100;// instance variables are stored in the Heapinti=10;publicvoidDisplay(){// local variables are stored in the Stackints=20;System.out.println(v);System.out.println(s);}}publicclassMain{publicstaticvoidmain(String[]args){Geeksg=newGeeks();// Calling the Display methodg.Display();}}

Output
10020

Note:

  • Static variables are stored in the Method Area.
  • Instance variables are stored in the Heap.
  • Local Variables are stored Stack.

3. JVM Stacks

  • A stack is created when a threadis created and the JVM stack is used to store method execution data, including local variables, method arguments and return addresses.
  • Each Thread has its own stack, ensuring thread safety.
  • Stacks size can be either fixed or dynamic and it can be set when the stack is created.
  • The memory for stack needs not to be contiguous.
  • Once a method completes execution, its associated stack frame is removed automatically.

4. Native Method Stacks

  • Native method stack is also known as C stacks.
  • Native method stacks are not written in Java language.
  • This memory is allocated for each thread when it is created and can have either a fixed or dynamic size.
  • Native method stacks handle the execution of native methods that interact with the Java code.

5. Program Counter (PC) Registers

  • Each JVM thread has a Program Counter (PC) register.
  • For non-native methods, it stores the address of the current JVM instruction.
  • For native methods, the PC value is undefined.
  • On some platforms, the PC can also store a return address or native pointer.

Heap vs Stack

Heap

Stack

Stores objects and instance variables.

Stores method calls and local variables.

Larger but slower.

Smaller but faster.

Random access.

LIFO (Last-In-First-Out).

Allocation is manual (using 'new' keyword), but deallocation is automatic.

Allocation and deallocation both are automatic.

Long-lived.

Short-lived (method duration).

Shared among all threads (needs synchronization).

Each thread has its own stack (thread safe).

Garbage Collector in java

The Garbage collector automatically removes the unused objects that are no longer needed. It runs in the background to free up memory.

  • Garbage collector finds objects that are no longer needed by the program.
  • It removes those unused objects to free up the memory and making space for new objects.
  • Java uses generational garbage collection so that new objects are collected more frequently in the young generation than older objects which survive longer in the old generation, this helps improve efficiency.
  • You can request garbage collection using System.gc(), but the JVM ultimately decides when it should run.

Note: You can request GC with System.gc() or Runtime.gc(), but JVM decides when it runs.


Java Memory Management
Improve
Improve
Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp