0% found this document useful (0 votes)
8 views5 pages

Java Memory Management

Java memory management is an automatic process handled by the JVM, involving memory allocation for new objects and garbage collection for unused objects. The Java Memory Model (JMM) consists of several areas including Stack, Heap, Method Area, PC Register, and Native Method Stack, each serving specific purposes. Garbage collection helps prevent memory leaks by identifying and removing unreferenced objects, ensuring efficient memory usage.

Uploaded by

Kirthi Kirthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Java Memory Management

Java memory management is an automatic process handled by the JVM, involving memory allocation for new objects and garbage collection for unused objects. The Java Memory Model (JMM) consists of several areas including Stack, Heap, Method Area, PC Register, and Native Method Stack, each serving specific purposes. Garbage collection helps prevent memory leaks by identifying and removing unreferenced objects, ensuring efficient memory usage.

Uploaded by

Kirthi Kirthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Memory Management

Memory management in java is an automatic process


handled by the JVM(Java Virtual Machine). It
involves in allocating memory for new objects and
removing the memory occupied objects that are no
longer in use. This process is primarily managed
through garbage collection.

Java uses a portion of memory called the Java


Memory Model (JMM) which is divided into several
areas:

swipe ->
Stack Area

Store temporary variables and separate memory


block for methods.
Store primitive data types.
Store reference of the heap objects
1. Strong reference
2. Weak reference
Soft reference
Each thread has its own stack memory.
Variables within a scope is only visible and as soon
as any variable goes out of the scope, it get deleted
from the stack(in LIFO order)
When stack memory goes full, its throws
"java.lang.StackOverflowError".
Heap Area
Store Objects
Ther is no order of allocating the memory.
Garbage Collector is used to delete the unreferenced
objects from the heap.
Mark and sweep algorithm
Types of GC:
1. single GC
2. parallel GC
3.CMS(concurrent mark sweep)
4.G1
Heap memory is shared with all the threads.
Heap also contains the string pool.
When heap memory goes ful, it throws
"java.lang.OutofMemoryError"
Heap memory is further divided into:
Young Generation(minor GC happens here)
1.Eden
2.survivor
Old Generation(major GC happens here)
Permanent Generation
Method Area

Stores class level data such as static variables, fields,


literals, methods & constructors.
Unlike the stack area, this is a shared memory area.
In older JVM’s it is named as Premgen, newer JVM’s it
use Metaspace.
Proper management ensures classes load efficiently
and static data is available throughout runtime.
PC register
Program Counter (PC) Register
Each thread has its own PC register to keep track of
the next instruction to execute.
It's a small memory register that stores the address
of the current instruction.
Native method stack
Used for executing native (non-Java) methods
written in languages like C or C++ (called via JNI -
Java Native Interface).
Separate from Java Stack: Manages native method
calls and their local variables.

Garbage Collection

Java uses a garbage collector (GC) to automatically


manage memory. It identifies and removes objects
that are no longer referenced by the program.
The GC runs in the background as a daemon
thread.
Garbage collection helps prevent memory leaks
and ensures efficient memory usage

You might also like