![]()
JVM 기반 애플리케이션을 운영하다보면 성능 이슈 때문에 JVM 메모리 관리를 알고 있어야 하는 경우가 많은데 이번 블로그에서는 JVM은 어떻게 메모리 관리를 하는지 알아보겠다.

Java의 메모리 모델은 크게 아래의 세가지로 나누어 진다.
stack은 method execution을 관리하는 공간이다. 이 공간에서는 method call frame, local variable, heap에 있는 object 참조 정보 등이 관리된다. stack은 thread별로 각각 가지고 있고, thread간 독립성을 보장한다.
public class Main {
public void methodA() {
int x = 5; // Stored in the stack
Car myCar = new Car(); // Reference is in the stack; object is in the heap
}
}
Heap은 동적 할당이 이루어지는 메모리 공간이다. 주로 runtime에 생성되는 객체들을 저장하는 역할을 한다.
그리고 객체들이 더 이상 참조되지 않으면 garbage collection에 의해 자동으로 정리된다.
class Car {
public String color;
}
public class Main {
public static void main(String[] args) {
new Car(); // Anew Car object Allocated on the heap, but its reference is not preserved! (not accesible/referenced) Garbage collection eligible):
Car myCar; // Reference type Varaiable (allocated on the stack) (pointing to null)
myCar = new Car(); // New Car object Allocated on the heap and its reference stored on myCar on the stack.
mycar.color = "Black"; // New String object allocated in heap and its reference is stored in instance reference variabl color of the object
}
}

metaspace에는 class의 메타데이터 즉, field 정보, method 이름과 constants등이 저장된다.

Heap, Stack, metaspace에 특징과 garbage collection이 해당 메모리 영역들에 어떻게 작동하는지 이해하는 것은 효율적인 Java apllcation을 만드는데 필수적이다. 그러므로 해당 블로그를 통해 JVM에서 메모리가 어떻게 관리되는지 잘 이해하자.
| Object class (0) | 2025.02.16 |
|---|---|
| Concurrent Random Number (0) | 2024.09.24 |
| ThreadLocal (1) | 2024.02.28 |
| Stream API 개요 (0) | 2023.05.05 |
| CompletableFuture get()과 join()의 차이점 (0) | 2023.05.02 |
댓글 영역