Java.Core.How does Java handle memory management?

☕️ How Java Handles Memory Management


🔗 1️⃣ Memory Areas in JVM

When you run a Java program, the JVM divides memory into several regions:

Memory AreaWhat it StoresExample
Method Area (part of Metaspace)Class metadata (names, methods, static fields, etc.)public static int count
HeapAll objects & instance variablesnew String(), new ArrayList()
StackLocal variables & method call dataint x = 10, object references
PC RegisterTracks current instruction for each threadBytecode instruction being executed
Native Method StackTracks native (non-Java) method callsNative I/O calls

🔥 2️⃣ Automatic Garbage Collection (GC)

In languages like C/C++, developers must manually allocate and free memory using malloc() and free(). In Java, the JVM does this for you via Garbage Collection (GC).


How does GC work?

  • When an object is no longer reachable (no reference pointing to it), it becomes eligible for GC.
  • GC runs in the background and reclaims memory by destroying unreachable objects.
  • This reduces the risk of memory leaks (but doesn’t eliminate them completely if you hold on to unnecessary references).

📦 3️⃣ Java Memory Model (Heap Division)

The Heap itself is divided into areas for more efficient GC.

SectionPurpose
Young GenerationFor new (short-lived) objects
Old Generation (Tenured)For long-lived objects
Eden SpaceWhere new objects are first allocated
Survivor SpacesObjects that survive GC cycles in Eden

Example

String s = new String("Hello");
  • s lives in the stack (as a reference).
  • The actual String object ("Hello") lives in the heap.

⚙️ 4️⃣ Garbage Collection Algorithms

Java has different GC algorithms, like:

GC TypeWhen UsedKey Benefit
Serial GCSingle-threaded appsSimple, works well for small heaps
Parallel GCMulti-core machinesUses multiple threads to speed up collection
G1 GC (Garbage First)Large heapsFocuses on predictable pause times
ZGC (Java 11+)Low-latency appsWorks with very large heaps (up to TB scale)

🚀 5️⃣ Finalization (rarely used)

If an object has a finalize() method, the GC may call it before destroying the object — but this is slow, unreliable, and generally discouraged. Better alternatives: try-with-resources or explicit cleanup methods.


⚠️ What You Still Need to Watch For

Memory Leaks in Java

  • Even though GC exists, you can still have logical memory leaks if you hold unnecessary references. Example:
List<Object> cache = new ArrayList<>();
  • If you forget to remove objects from cache, even if the objects are not needed anymore, they will not be garbage collected.

OutOfMemoryError (OOM)

  • Happens when:
    • Heap is too small for all objects.
    • Too many large objects are held for too long.
    • Memory leak exists in your code.

💡 Summary Diagram





JVM Memory
+--------------------+
| Method Area        | <- class metadata, static fields
|                    |
+--------------------+
| Heap               | <- objects (new)
|  - Young Gen       |
|  - Old Gen         |
+--------------------+
| Stack (per thread) | <- method calls, local vars
+--------------------+
| PC Register        | <- current bytecode instruction
+--------------------+
| Native Stack       | <- native method calls
+--------------------+

🎯 Quick Recap

ProcessWho Handles It?
Object creationYou (with new)
Object destructionJVM (via Garbage Collector)
Memory allocationJVM
Memory freeingJVM
This entry was posted in Без рубрики. Bookmark the permalink.