SlideShare a Scribd company logo
1 of 23
Download to read offline
Java 이틀째
TmaxSoft R&D Center
1
113년	 10월	 10일	 목
오늘 내용
Hello, World & Review
finalize and reference objects
Garbage Collections
java.util.Collection
2
213년	 10월	 10일	 목
Hello, World
HelloWorld.java
one public outer class in one source file
entry point : public static void main(String[])
package name
classpath
javac : bytecode compiler (.java -> .class)
java : virtual machine launcher
-server (sever VM)
3
313년	 10월	 10일	 목
constructor
HelloWorld hello = new HelloWorld();
자바는 stack에 객체를 만들 수 없다. 항상 heap에 allocate
4
413년	 10월	 10일	 목
destructor? finalizer!
protected void finalize() throws Throwable;
JVM이 자동적으로 호출
user code에서 직접 호출하는 것은 JVM과 무관
5
513년	 10월	 10일	 목
finalizer 관점에서 Object 상태
reachable 관점 (다른 객체에서 access할 수 있느냐)
1. reachable : live thread에서 언제든지 액세스할 수 있는 상태
2. finalizer-reachable : finalizable 객체에서 결과적으로 액세스할 수 있으나 live
thread에서는 액세스할 수 없는 상태
3. unreachable : finalizable 객체도 액세스할 수 없는 상태
finalized 관점 (finalizer가 완료되었느냐)
1. unfinalized : VM이 finalizer를 호출하지 않은 상태
2. finalizable : VM이 finalizer를 호출할 수 있는 상태
3. finalized : VM이 finalizer를 호출한 상태
6
613년	 10월	 10일	 목
more on reachables
java.lang.ref 패키지
SoftReference : gc가 메모리 부족 판
단 시 제거. 메모리에 민감한 캐시 용도
WeakReference : gc가 언제든지
finalize. 주로 WeakHashMap 구현을
위해 필요
PhantomReference : gc로부터
reachable 상태를 벗어난 객체로부터
noti를 받기 위함 (finalizer 대체)
7
strongly reachable
softly reachable
weakly reachable
phantom reachable
unreachable
713년	 10월	 10일	 목
ReferenceQueue
각 Reference들이 해당 reachable 상태에 이르면 garbage
collector가 지정된 ReferenceQueue로 enqueue
Soft/WeakReference의 경우는 referent object가 Softly/
Weakly Reachable의 상태에서 gc에 의해 자동으로 clear되
지만, PhantomReference의 경우는 Phantom Reachable
의 상태에서 명시적으로 clear되어야 한다.
PhantomReference의 referent는 enqueue될 때
reclaimable 상태여야 하므로 이를 get할 수 없다.
8
813년	 10월	 10일	 목
Garbage Collection
live object가 아닌 dead object를 garbage
기본 알고리즘
serial vs parallel
concurrent vs stop-the-world
compacting vs non-compacting vs copying
non-compacting releases object in-place and can
cause fragmentation
9
913년	 10월	 10일	 목
HotSpot Memory Model
Object references are direct pointers
gc will update the pointer on relocation
Two-word Object Headers (array는 3-word)
identity hash code and GC status information
a reference to the object's class
[array size]
Object packing : reordering fields
10
1013년	 10월	 10일	 목
Basics on HotSpot JVM GC
Conventional Generational Collection (Before JDK 7 G1 GC)
11
PERMANENT
1113년	 10월	 10일	 목
young to old promotion
처음에 객체가 만들어지면 Eden에 위치 (일부 큰 객체는 바로
old로 갈 수도 있음)
한번의 young generation collection(minor gc)를 넘어가면
survivor space 둘 중 하나로 이동 (항상 두 survivor space
중 하나만 사용됨)
survivor space에는 tenuring threshold를 넘을 때까지 위치
할 수 있음 (매 collection마다 다른 space로 이동되거나
tenure로 promotion)
12
1213년	 10월	 10일	 목
Young Generation Collector
serial collector (default in Client JVM)
-XX:+UseSerialGC
parallel collector (thruput collector)
-XX:+UseParallelGC
-XX:+UseParallelOldGC (old 에도 parallel 적용)
concurrent collector (shorter gc pause time)
-XX:+UseConcMarkSweepGC
minor, major 모두 적용되며 minor collection에서도 짧은 pause가 발생
large tenured data set에서 많이 사용
13
1313년	 10월	 10일	 목
System.gc(), RMI
14
1413년	 10월	 10일	 목
G1(Garbage First) GC
since JDK 7u4
–XX:+UseG1GC
regions : 1MB~32MB
incremental parallel
compacting GC
가장 garbage가 많은
region부터 collect
more predictable
pause times
15
1. initial mark phase : mark
root and young gc (STW)
2. root region scanning
phase : survivor to old
3. concurrent mark phase :
find reachable across
entire heap
4. remark phase :
completing marks
5. cleanup phase : free all-
dead region (partly STW)
1513년	 10월	 10일	 목
java.util.Collection
16
Collection
List Set
SortedSet
NavigableSet
Map
SortedMap
NavigableMap
Queue
Deque
1613년	 10월	 10일	 목
Collections
Interfaces
Collection, Set, List, Queue
Map
equals/hashCode
Iterator/ListIterator
Collections class (cf. Arrays class)
Vector, Hashtable, Enumeration
Wrappers : unmodifiable, synchronized, checked
17
1713년	 10월	 10일	 목
Collection Classes
List : ArrayList, LinkedList, Vector
Map : TreeMap (red-black tree), HashMap, Hashtable
Set : TreeSet, HashSet
Queue : PriorityQueue (NOTE that PriorityQueue.iterator()
does not provide ordered traversal)
Deque : ArrayDeque
NavigableMap : TreeMap
NavigableSet : TreeSet
18
1813년	 10월	 10일	 목
Iterator
Fail-fast algorithm
Iterating Map
keySet
entrySet : Map.Entry
values
For-each and collection
19
1913년	 10월	 10일	 목
Fail-fast
ArrayList<String> list = ...
DO
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
String v = iter.next();
if (“abc”.equals(v)) {
iter.remove();
}
}
20
ArrayList<String> list = ...
DO NOT
Iterator<String> iter =
list.iterator();
while (iter.hasNext()) {
String v = iter.next();
list.remove(“abc”);
}
2013년	 10월	 10일	 목
DO NOT
HashMap<String, String> map = ...;
DO NOT
for (String key : map.keySet()) {
String v = map.get(key);
}
DO
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String v = entry.getValue();
}
21
2113년	 10월	 10일	 목
Syntactic Sugars
Autobox/unbox
For-each
enum
22
2213년	 10월	 10일	 목
다음 시간 예정
Thread
wait/notify
Lock/Condition
volatile
final
Java Memory Model
23
2313년	 10월	 10일	 목

More Related Content

What's hot

[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스종빈 오
 
Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본ssuser0c2478
 
windows via c++ Ch 5. Job
windows via c++ Ch 5. Jobwindows via c++ Ch 5. Job
windows via c++ Ch 5. JobHyosung Jeon
 
Effective STL 1~4장 정리
Effective STL 1~4장 정리Effective STL 1~4장 정리
Effective STL 1~4장 정리Shin heemin
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinDong Chan Shin
 
자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스Lee Dong Wook
 
02 realm 모델 &amp; 관계
02   realm 모델 &amp; 관계02   realm 모델 &amp; 관계
02 realm 모델 &amp; 관계Lee-Jong-Chan
 
Free rtos seminar
Free rtos seminarFree rtos seminar
Free rtos seminarCho Daniel
 
Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706Yong Joon Moon
 
Scope and Closure of JavaScript
Scope and Closure of JavaScript Scope and Closure of JavaScript
Scope and Closure of JavaScript Dahye Kim
 
11 윈도우스레드풀
11 윈도우스레드풀11 윈도우스레드풀
11 윈도우스레드풀ssuser0c2478
 
Realm 코딩카페 - 이종찬
Realm   코딩카페 - 이종찬Realm   코딩카페 - 이종찬
Realm 코딩카페 - 이종찬Lee-Jong-Chan
 
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http RequestNAVER D2
 
Java programming pdf
Java programming pdfJava programming pdf
Java programming pdfJi Hoon Lee
 
07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도ssuser3fb17c
 

What's hot (20)

[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스[Windows via c/c++] 4장 프로세스
[Windows via c/c++] 4장 프로세스
 
Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본Windows via C/C++ 06 스레드의 기본
Windows via C/C++ 06 스레드의 기본
 
Java 8 고급 (5/6)
Java 8 고급 (5/6)Java 8 고급 (5/6)
Java 8 고급 (5/6)
 
windows via c++ Ch 5. Job
windows via c++ Ch 5. Jobwindows via c++ Ch 5. Job
windows via c++ Ch 5. Job
 
Effective STL 1~4장 정리
Effective STL 1~4장 정리Effective STL 1~4장 정리
Effective STL 1~4장 정리
 
Java 8 고급 (1/6)
Java 8 고급 (1/6)Java 8 고급 (1/6)
Java 8 고급 (1/6)
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshin
 
자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스자바스크립트 프로토타입 및 클래스
자바스크립트 프로토타입 및 클래스
 
02 realm 모델 &amp; 관계
02   realm 모델 &amp; 관계02   realm 모델 &amp; 관계
02 realm 모델 &amp; 관계
 
Free rtos seminar
Free rtos seminarFree rtos seminar
Free rtos seminar
 
Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706Jupyter notebok tensorboard 실행하기_20160706
Jupyter notebok tensorboard 실행하기_20160706
 
Scope and Closure of JavaScript
Scope and Closure of JavaScript Scope and Closure of JavaScript
Scope and Closure of JavaScript
 
11 윈도우스레드풀
11 윈도우스레드풀11 윈도우스레드풀
11 윈도우스레드풀
 
Realm 코딩카페 - 이종찬
Realm   코딩카페 - 이종찬Realm   코딩카페 - 이종찬
Realm 코딩카페 - 이종찬
 
Anatomy of Realm
Anatomy of RealmAnatomy of Realm
Anatomy of Realm
 
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
 
Java programming pdf
Java programming pdfJava programming pdf
Java programming pdf
 
07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도07 스레드스케줄링,우선순위,그리고선호도
07 스레드스케줄링,우선순위,그리고선호도
 
Clean code appendix 1
Clean code appendix 1Clean code appendix 1
Clean code appendix 1
 
MutiCore 19-20
MutiCore 19-20MutiCore 19-20
MutiCore 19-20
 

More from Kyung Koo Yoon

Spring Framework - Inversion of Control Container
Spring Framework - Inversion of Control ContainerSpring Framework - Inversion of Control Container
Spring Framework - Inversion of Control ContainerKyung Koo Yoon
 
Smart software engineer
Smart software engineerSmart software engineer
Smart software engineerKyung Koo Yoon
 
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.Kyung Koo Yoon
 
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.Kyung Koo Yoon
 
Lecture on Java Concurrency Day 4 on Feb 18, 2009.
Lecture on Java Concurrency Day 4 on Feb 18, 2009.Lecture on Java Concurrency Day 4 on Feb 18, 2009.
Lecture on Java Concurrency Day 4 on Feb 18, 2009.Kyung Koo Yoon
 
Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.Kyung Koo Yoon
 
창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어Kyung Koo Yoon
 

More from Kyung Koo Yoon (10)

Kubernetes
Kubernetes Kubernetes
Kubernetes
 
Java 8 고급 (6/6)
Java 8 고급 (6/6)Java 8 고급 (6/6)
Java 8 고급 (6/6)
 
Java 8 고급 (4/6)
Java 8 고급 (4/6)Java 8 고급 (4/6)
Java 8 고급 (4/6)
 
Spring Framework - Inversion of Control Container
Spring Framework - Inversion of Control ContainerSpring Framework - Inversion of Control Container
Spring Framework - Inversion of Control Container
 
Smart software engineer
Smart software engineerSmart software engineer
Smart software engineer
 
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
 
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
 
Lecture on Java Concurrency Day 4 on Feb 18, 2009.
Lecture on Java Concurrency Day 4 on Feb 18, 2009.Lecture on Java Concurrency Day 4 on Feb 18, 2009.
Lecture on Java Concurrency Day 4 on Feb 18, 2009.
 
Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.
 
창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어
 

Java 8 고급 (2/6)

  • 1. Java 이틀째 TmaxSoft R&D Center 1 113년 10월 10일 목
  • 2. 오늘 내용 Hello, World & Review finalize and reference objects Garbage Collections java.util.Collection 2 213년 10월 10일 목
  • 3. Hello, World HelloWorld.java one public outer class in one source file entry point : public static void main(String[]) package name classpath javac : bytecode compiler (.java -> .class) java : virtual machine launcher -server (sever VM) 3 313년 10월 10일 목
  • 4. constructor HelloWorld hello = new HelloWorld(); 자바는 stack에 객체를 만들 수 없다. 항상 heap에 allocate 4 413년 10월 10일 목
  • 5. destructor? finalizer! protected void finalize() throws Throwable; JVM이 자동적으로 호출 user code에서 직접 호출하는 것은 JVM과 무관 5 513년 10월 10일 목
  • 6. finalizer 관점에서 Object 상태 reachable 관점 (다른 객체에서 access할 수 있느냐) 1. reachable : live thread에서 언제든지 액세스할 수 있는 상태 2. finalizer-reachable : finalizable 객체에서 결과적으로 액세스할 수 있으나 live thread에서는 액세스할 수 없는 상태 3. unreachable : finalizable 객체도 액세스할 수 없는 상태 finalized 관점 (finalizer가 완료되었느냐) 1. unfinalized : VM이 finalizer를 호출하지 않은 상태 2. finalizable : VM이 finalizer를 호출할 수 있는 상태 3. finalized : VM이 finalizer를 호출한 상태 6 613년 10월 10일 목
  • 7. more on reachables java.lang.ref 패키지 SoftReference : gc가 메모리 부족 판 단 시 제거. 메모리에 민감한 캐시 용도 WeakReference : gc가 언제든지 finalize. 주로 WeakHashMap 구현을 위해 필요 PhantomReference : gc로부터 reachable 상태를 벗어난 객체로부터 noti를 받기 위함 (finalizer 대체) 7 strongly reachable softly reachable weakly reachable phantom reachable unreachable 713년 10월 10일 목
  • 8. ReferenceQueue 각 Reference들이 해당 reachable 상태에 이르면 garbage collector가 지정된 ReferenceQueue로 enqueue Soft/WeakReference의 경우는 referent object가 Softly/ Weakly Reachable의 상태에서 gc에 의해 자동으로 clear되 지만, PhantomReference의 경우는 Phantom Reachable 의 상태에서 명시적으로 clear되어야 한다. PhantomReference의 referent는 enqueue될 때 reclaimable 상태여야 하므로 이를 get할 수 없다. 8 813년 10월 10일 목
  • 9. Garbage Collection live object가 아닌 dead object를 garbage 기본 알고리즘 serial vs parallel concurrent vs stop-the-world compacting vs non-compacting vs copying non-compacting releases object in-place and can cause fragmentation 9 913년 10월 10일 목
  • 10. HotSpot Memory Model Object references are direct pointers gc will update the pointer on relocation Two-word Object Headers (array는 3-word) identity hash code and GC status information a reference to the object's class [array size] Object packing : reordering fields 10 1013년 10월 10일 목
  • 11. Basics on HotSpot JVM GC Conventional Generational Collection (Before JDK 7 G1 GC) 11 PERMANENT 1113년 10월 10일 목
  • 12. young to old promotion 처음에 객체가 만들어지면 Eden에 위치 (일부 큰 객체는 바로 old로 갈 수도 있음) 한번의 young generation collection(minor gc)를 넘어가면 survivor space 둘 중 하나로 이동 (항상 두 survivor space 중 하나만 사용됨) survivor space에는 tenuring threshold를 넘을 때까지 위치 할 수 있음 (매 collection마다 다른 space로 이동되거나 tenure로 promotion) 12 1213년 10월 10일 목
  • 13. Young Generation Collector serial collector (default in Client JVM) -XX:+UseSerialGC parallel collector (thruput collector) -XX:+UseParallelGC -XX:+UseParallelOldGC (old 에도 parallel 적용) concurrent collector (shorter gc pause time) -XX:+UseConcMarkSweepGC minor, major 모두 적용되며 minor collection에서도 짧은 pause가 발생 large tenured data set에서 많이 사용 13 1313년 10월 10일 목
  • 15. G1(Garbage First) GC since JDK 7u4 –XX:+UseG1GC regions : 1MB~32MB incremental parallel compacting GC 가장 garbage가 많은 region부터 collect more predictable pause times 15 1. initial mark phase : mark root and young gc (STW) 2. root region scanning phase : survivor to old 3. concurrent mark phase : find reachable across entire heap 4. remark phase : completing marks 5. cleanup phase : free all- dead region (partly STW) 1513년 10월 10일 목
  • 17. Collections Interfaces Collection, Set, List, Queue Map equals/hashCode Iterator/ListIterator Collections class (cf. Arrays class) Vector, Hashtable, Enumeration Wrappers : unmodifiable, synchronized, checked 17 1713년 10월 10일 목
  • 18. Collection Classes List : ArrayList, LinkedList, Vector Map : TreeMap (red-black tree), HashMap, Hashtable Set : TreeSet, HashSet Queue : PriorityQueue (NOTE that PriorityQueue.iterator() does not provide ordered traversal) Deque : ArrayDeque NavigableMap : TreeMap NavigableSet : TreeSet 18 1813년 10월 10일 목
  • 19. Iterator Fail-fast algorithm Iterating Map keySet entrySet : Map.Entry values For-each and collection 19 1913년 10월 10일 목
  • 20. Fail-fast ArrayList<String> list = ... DO Iterator<String> iter = list.iterator(); while (iter.hasNext()) { String v = iter.next(); if (“abc”.equals(v)) { iter.remove(); } } 20 ArrayList<String> list = ... DO NOT Iterator<String> iter = list.iterator(); while (iter.hasNext()) { String v = iter.next(); list.remove(“abc”); } 2013년 10월 10일 목
  • 21. DO NOT HashMap<String, String> map = ...; DO NOT for (String key : map.keySet()) { String v = map.get(key); } DO for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String v = entry.getValue(); } 21 2113년 10월 10일 목