Git Product home page Git Product logo

study's People

Watchers

 avatar  avatar

study's Issues

Java Garbage Collection

https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

GC Basic

  • 3 basic operations in order
    1. finding unused objects : black rectangles
    2. making their memory available
    3. compacting the heap
    • image
  • Application thread vs GC thread
    • GC thread가 memory 공간을 수정할 때 application thread는 메모리를 할당하지 못하거나 이미 할당된 Object를 사용하지 못함 : stop-the-world pause
  • GC는 JVM 양날의 검 : 알아서 메모리 관리를 해주므로 편리하지만 GC에 대해 너무 모르면 뒷통수를 맞게 됨

OutOfMemoryError(OOM, OOME)

  • Application thread가 new를 호출했을 때 또는 새로운 class를 loading할 때 발생할 수 있다.
  • OOM이 발생하면 다 memory leak인가요?
    • byte[] x = new byte[1073741824], byte[] x = new byte[size] : JVM이 메모리 확보를 하지 못하면 OOME 발생. 해당 thread에만 국한된 문제로 memory leak이 아님
  • memory leak인지 아닌지 판별하기 위해 heap dump로 분석 필요
    • heap memory가 부족한 것이 아닌가?
    • application bug로 인해 memory leak이 발생한 경우
  • heapdump with jcmd
    • jcmd : java process list
    • jcmd [PID] GC.heap_dump [PATH]

오브젝트에 대한 레퍼런스 관리는 어떻게 하는가?

  • Allocation된 메모리 덩어리(chunk)들을 Tree로 관리하고, GC 할 때 Tree Traversal을 통해서 Reachability Check
  • Strong Reference vs Weak Reference 개념만 이해하면 충분 : 본인 경험상 Soft/Weak/Phantom Reference를 사용할 일이 없었음.
    • GC는 확률과 통계에 기반해서 동작하는 알고리즘이기 때문에 Strong Reference를 제외하면 100% 보장하는 것이 없음
    • GC에 기반해서 프로그램을 짜면 안 된다고 생각함
  • https://d2.naver.com/helloworld/329631

Generational GC

  • Young generation and Old(Tenured) Generation
    • younggen vs oldgen
  • 대부분의 object들은 쓰고 바로 버린다는 통계적인 근거에 기반해서 개발한 알고리즘
    image
  • GC는 stop-the-worlds operation 이기 때문에 무조건 pause가 발생
    • stop(pause) 시간을 최소화하는 것이 목표

Young generation

  • Eden and the Survivor spaces 으로 구성
    image
  • object allocation은 항상 Eden에서 일어남
  • gc는 기본적으로 younggen에 대해서 수행 : minor GC 라고도 부름
    • younggen GC에서 살아남은 object들은 oldgen으로 이동
    • younggen GC를 해도 할당해야 할 메모리가 부족하면 oldgen GC 수행 : major 또는 full GC 라고 부름

minor GC 전후

Four GC Algorithms

Java class loader 알아보기

https://www.baeldung.com/java-classloaders

JVM Default Class Loaders

clhierarchy

User-Defined Class Loaders

  • 모든 class loader 구현은 반드시 java.lang.ClassLoader abstract class를 상속해야 한다.

  • [주의] Class Loader 관계는 OOP 상속 아님! 클래스 로딩 모델인 delegation 을 위해서 class loader 간의 parent/child 관계를 지정해주는 것.

  • 구현 편의상 보통 java.net.URLClassLoader를 상속한다.

  • [Example] Hadoop RunJar

class_loaders

  • Let’s focus on two keywords; on-demand loading, delegation

On-demand Loading & Delegation

  • On-demand Loading : 어떤 클래스가 필요한 시점에 실제로 로딩(=Class 인스턴스 생성)
    • 미리 모든 클래스를 로딩한다면? 대량의 메모리를 필요로 하며 부팅 타임도 증가
  • Delegation : 상위 클래스 로더에 클래스 로딩을 맡기고, 없으면(=ClassNotFoundException이 발생하면) 직접 로딩 시도하는 방식
  • [Example Again] Hadoop RunJar

export HADOOP_CLASSPATH=hadoop-xxxx.jar;.....;hive-....jar;...
java -classpath hadoop-common.jar;..... org.apache.hadoop.util.RunJar org.apache.hive.service.server.HiveServer2

https://github.com/c9n/hadoop/blob/a315107901aebc8d0c2b9f530d191673d27922a2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/RunJar.java#L135-L225

  1. JVM이 main class인 org.apache.hadoop.util.RunJar 로딩 : RunJar 뒤의 값들은 main 함수로 넘어가는 arguments
  2. main 함수에서 HADOOP_CLASSPATH 경로 구성 후 System class loader 하위의 새로운 class loader 생성 : 설명 편의상 Hadoop class loader라고 명명.
  3. Hadoop class loader에서 HiveServer2 클래스 로딩 후 HiveServer2의 main 함수를 reflection으로 호출

More on On-demand Loading

  • JVM이 이미 로딩한 어떤 class의 코드를 실행할 때 '새로운'(link 정보가 없는) class를 발견하면, 그 class에 대한 java.lang.Class instance를 얻기 위해서 로딩
  • JVM이 로딩할 때 어떤 클래스 로더를 사용하나?
  • 이미 로딩한 class에 대한 로딩 요청이 또 오면?
    • class defining이 비싼 작업이기 때문에 Class Instance Cache 필수
    • 왜 비싼 작업인지, Cache 구현을 어떻게 할지 자세한 사항은 ClassLoader.loadClass Flow에서 설명
    • Class Instance 별로 JVM이 JIT 컴파일 코드 생성 및 Cache
    • Class Instance Cache가 있으므로 concurrency를 높이기 위한 성능 고려가 매우 중요

More on Delegation

More Class Loader Hierarchy Examples

  • Hadoop Runjar
  • WAS Enterprise Application

ClassLoader.loadClass Flow

cache
https://blog.hargrave.io/2007/09/classforname-caches-defined-class-in.html?m=1

IBM J9

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.