study's People
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
- finding unused objects : black rectangles
- making their memory available
- compacting the heap
- 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 listjcmd [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들은 쓰고 바로 버린다는 통계적인 근거에 기반해서 개발한 알고리즘
- GC는 stop-the-worlds operation 이기 때문에 무조건 pause가 발생
- stop(pause) 시간을 최소화하는 것이 목표
Young generation
- Eden and the Survivor spaces 으로 구성
- object allocation은 항상 Eden에서 일어남
- gc는 기본적으로 younggen에 대해서 수행 : minor GC 라고도 부름
- younggen GC에서 살아남은 object들은 oldgen으로 이동
- younggen GC를 해도 할당해야 할 메모리가 부족하면 oldgen GC 수행 : major 또는 full GC 라고 부름
minor GC 전후
- From https://d2.naver.com/helloworld/1329
- 메모리 복사, 가리키는 위치 수정 등을 해야 하므로 thread들을 stop 해야 함.
Four GC Algorithms
Java class loader 알아보기
https://www.baeldung.com/java-classloaders
JVM Default Class Loaders
- Java Endorsed Standards Override Mechanism
- https://docs.oracle.com/javase/8/docs/technotes/guides/standards/index.html
- /lib/endorsed 에 jar를 두면 rt.jar보다 우선 순위를 높여서 로딩
- 8에서 This feature is deprecated and will be removed in a future release. 라고 되어 있지만 사용 가능
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
- 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
- JVM이 main class인 org.apache.hadoop.util.RunJar 로딩 : RunJar 뒤의 값들은 main 함수로 넘어가는 arguments
- main 함수에서 HADOOP_CLASSPATH 경로 구성 후 System class loader 하위의 새로운 class loader 생성 : 설명 편의상 Hadoop class loader라고 명명.
- Hadoop class loader에서 HiveServer2 클래스 로딩 후 HiveServer2의 main 함수를 reflection으로 호출
More on On-demand Loading
- JVM이 이미 로딩한 어떤 class의 코드를 실행할 때 '새로운'(link 정보가 없는) class를 발견하면, 그 class에 대한 java.lang.Class instance를 얻기 위해서 로딩
- JVM이 로딩할 때 어떤 클래스 로더를 사용하나?
- Formal rule : https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html#jvms-5.4.3.1
- Let's see RunJar Example again.
- RunJar main method를 실행하는 과정(static scope), main method 안에서 new RunJar() 했을 때 RunJar에 선언된 field 상의 class들은 누굴 통해서 loading 할까? System class loader
- HiveServer2.main() 이 호출되고 나서 HiveServer2 코드를 실행하는 과정에서는? Hadoop class loader
- https://stackoverflow.com/questions/1771679/difference-between-threads-context-class-loader-and-normal-classloader/36228195#36228195
- 이미 로딩한 class에 대한 로딩 요청이 또 오면?
- class defining이 비싼 작업이기 때문에 Class Instance Cache 필수
- 왜 비싼 작업인지, Cache 구현을 어떻게 할지 자세한 사항은 ClassLoader.loadClass Flow에서 설명
- Class Instance 별로 JVM이 JIT 컴파일 코드 생성 및 Cache
- Class Instance Cache가 있으므로 concurrency를 높이기 위한 성능 고려가 매우 중요
More on Delegation
- 새로운 ClassLoader instance 만들 때 parent class loader 지정
https://github.com/openjdk/jdk/blob/25c5a23695a8d900c2b8c5ad79d2b09c336467d5/src/java.base/share/classes/java/lang/ClassLoader.java#L457-L459 - 지정하지 않을 경우에는 JVM system class loader
https://github.com/openjdk/jdk/blob/25c5a23695a8d900c2b8c5ad79d2b09c336467d5/src/java.base/share/classes/java/lang/ClassLoader.java#L1921-L1940 - delegation 방식 & 규칙
- 먼저 parent class loader한테 물어본다. (parent.loadClass() 호출)
- 없으면 (parent.loadClass()에서 ClassNotFoundException 발생) 나에게서 찾는다.
- 나한테도 없으면 ClassNotFoundException 을 던진다.
- 왜 이런 규칙이 있는가? 왜 parent부터 로딩하는가?
- 규칙을 안 따르면 안 되는가? 즉 parent에게 먼저 물어보지 않으면 JVM에서 에러가 발생하는가? No.
- 나에게서 먼저 찾고 없으면 parent에게 물어보도록 만들어도 된다. Tomcat webapp class loader : https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
https://github.com/apache/tomcat/blob/892353a6e528be27f114b2496fa8a420c03bcdba/java/org/apache/catalina/loader/WebappClassLoaderBase.java#L1218-L1366
- 나에게서 먼저 찾고 없으면 parent에게 물어보도록 만들어도 된다. Tomcat webapp class loader : https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.