Post

JVM 관련

JVM 전체 구조

https://coding-factory.tistory.com/828

  • Class Loader
    • 런타임(최초 호출 시점)에 .class(바이트코드) 읽어 class load
  • Execution Engine
    • Interpreter, JIT 둘 다 사용
  • Memory Layout
    • PC register
      • JVM level에서 현재 실행하고 있는 instruction의 주소 보관.
      • CPU level의 PC와 기능은 같지만 추상화 수준이 다름.
      • thread 별로 각각 가지고 있음. (CPU register가 thread local 한 것 처럼 당연히)
    • Method Area
      • Memory layout 상 PermGen, MetaSpace 안에 속하는 일부 영역.
      • OS의 text segment와 유사함. (결국 실행 대상 code, instructions를 보관하는 영역이라는 뜻)
      • run-time constant pool
      • field and method data
      • the code for methods and constructors

**참고) static 변수는 Method Area 안에 들어가는게 아니라(JVM spec에 이런 내용은 없다),
Method Area를 감싸고 있는 PermGen 영역에 들어간다. (이 것도 java 8 부터는 PermGen이 사라지면서 달라졌다.)

Method Area 등은 JVM spec 에서 사용하는 용어이고,
실제 구현체인 Hotspot JVM에서는 이와 1:1 대응되지 않는 다른 용어(PermGen, Metaspace)를 사용 할 수 있다.
스펙과 실제 구현이 다를 수 있기 때문에 스펙 자체에 뭐가 들어가고 뭐가 안들어가고를 너무 타이트하게 볼 필요는 없다.

JVM Heap 구조와 GC

JVM PermGen과 MetaSpace

https://www.digitalocean.com/community/tutorials/java-jvm-memory-model-memory-management-in-java (GC 구간 참고)

** 참고) PermGen에서 저장하고 있는 static 변수는 reference 만이다. Object itself가 아니다. ( sof ) 동적배열 static 변수가 PermGen 고갈을 유발한다고 볼 수는 없다.

변수, 객체의 resolve 시점?

  • compile time에 resolve 되는 것
    • 상수,Constant Expression 뿐이다. (조건을 만족하는 String, primitive type들만 해당)
    • Compile-time constant expressions of type String are always “interned” so as to share unique instances, using the method String.intern()
  • static 변수는? Runtime : 최초 호출 시점에 resolve
    • 예상과 달리 Application 실행 시점에 자동으로 resolve 되는 것이 아니다.
    • 해당 클래스 최초 접근 시점에 class loader가 .class 파일 읽어 load 하면서 metadata도 메모리에 올라가기 때문.
  • Class 로딩과 초기화는 다르다.
    • 상기 JVM 구조에서, Class Loader가 클래스를 로드하는 과정은 3가지 sub-step으로 구성된다.
      1. Loading -Cls.class 접근하는 순간 발생하며 .class 파일 읽어와 Method Area에 올린다.
      2. Linking - 검증 작업
      3. Initialization -Cls 생성자나 상수가 아닌 static 멤버에 접근하는 순간(==resolve가 필요한 순간)발생하며 static 필드 초기화, static 블럭 수행
    • 이 과정은 thread-safe 하다. (동시에 여러 thread에서 여러번 초기화 되지 않는다)
    • 어떻게 접근하느냐에 따라 Loading까지만 일어나고 static 초기화는 아직 일 수도 있지만, 보통은 Cls.class만 하는 경우가 잘 없으므로 3가지 과정이 연속해서 일어난다고 보면 된다.
This post is licensed under CC BY 4.0 by the author.