Garbage Collection

JVM ์˜ Garbage Collector ๊ฐ€ Unreachable Object ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐํ•˜์—ฌ ๊ณต๊ฐ„์„ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ

stop the world

  • GC ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด JVM ์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ž‘๋™์„ ๋ฉˆ์ถ”๋Š” ๊ฒƒ์œผ๋กœ ์–ด๋–ค GC ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๋„ stop the world ๋Š” ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

  • stop the world ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด GC ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์“ฐ๋ ˆ๋“œ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์“ฐ๋ ˆ๋“œ๋Š” ๋ชจ๋‘ ์ž‘์—…์„ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.

  • ๋Œ€๊ฐœ์˜ ๊ฒฝ์šฐ GC ํŠœ๋‹์ด๋ž€ ์ด stop the world ์‹œ๊ฐ„์„ ์ค„์ด๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

System.gc()

๋ช…์‹œ์ ์œผ๋กœ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์ด ์ผ์–ด๋‚˜๊ฒŒ ํ•˜์ง€๋งŒ, ๋ชจ๋“  ์Šค๋ ˆ๋“œ๊ฐ€ ์ค‘๋‹จ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ํ†ตํ•œ ํ˜ธ์ถœ์€ ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Garbage Collection ๊ณผ์ •

Mark Sweep Compaction

  • Mark

    • JVM ์˜ Garbage Collector ๊ฐ€ ์Šคํƒ์˜ ๋ชจ๋“  ๋ณ€์ˆ˜๋ฅผ ์Šค์บ”ํ•˜๋ฉด์„œ ๊ฐ๊ฐ ์–ด๋–ค ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ ˆํผ๋Ÿฐ์Šค ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ฐพ๋Š” ๊ณผ์ •

    • reachable ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ๋ ˆํผ๋Ÿฐ์Šคํ•˜๊ณ  ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ๋ฅผ marking ํ•ฉ๋‹ˆ๋‹ค.

    • ๋ชจ๋“  ์Šค๋ ˆ๋“œ์˜ ์ž‘์—…์ด ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. (stop the world)

  • Sweep

    • mark ๋˜์–ด์žˆ์ง€ ์•Š์€ ๋ชจ๋“  ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํž™์—์„œ ์ œ๊ฑฐํ•˜๋Š” ๊ณผ์ •

    • Garbage Collection ์ด๋ผ๊ณ  ํ•˜๋ฉด garbage ๋“ค์„ ์ˆ˜์ง‘ํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” garbage ๊ฐ€ ์•„๋‹Œ ๊ฒƒ์„ ๋”ฐ๋กœ mark ํ•˜๊ณ  ๊ทธ ์™ธ์˜ ๊ฒƒ์€ ๋ชจ๋‘ ์ง€์›๋‹ˆ๋‹ค.

  • Compact

    • sweep ๋‹จ๊ณ„ ํ›„ ์‚ด์•„๋‚จ์€ ๊ฐ์ฒด๋“ค ์‚ฌ์ด์‚ฌ์ด์˜ ๋นˆ ๊ณต๊ฐ„์„ ์ฑ„์šฐ๊ณ  ์ฐธ์กฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์—์„œ overhead ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

Heap Garbage Collection

  • MinorGC: Young Generation ์—์„œ ๋ฐœ์ƒํ•˜๋Š” GC

  • MajorGC: Old Generation (Tenured Space) ์—์„œ ๋ฐœ์ƒํ•˜๋Š” GC

  • FullGC: heap ์ „์ฒด๋ฅผ clear ํ•˜๋Š” ์ž‘์—… (young/old ๊ณต๊ฐ„ ๋ชจ๋‘)

  • Metaspace: java 1.8 ์ดํ›„ Permanent Generation ์—์„œ ๋ณ€๊ฒฝ๋œ ๋ช…์นญ์ž…๋‹ˆ๋‹ค. static ๋ณ€์ˆ˜, ์ƒ์ˆ˜๊ฐ€ heap ์œผ๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ์ƒˆ๋กœ์šด ์˜ค๋ธŒ์ ํŠธ๋Š” Eden ์— ํ• ๋‹น๋˜๊ณ , ๋‘ ๊ฐœ์˜ Survivor Space ๋Š” ๋น„์›Œ์ง„ ์ƒํƒœ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  2. Eden ์ด ๊ฐ€๋“์ฐจ๋ฉด, MinorGC ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

    1. reachable ์˜ค๋ธŒ์ ํŠธ๋“ค์€ S0 ์œผ๋กœ ์˜ฎ๊ฒจ์ง€๊ณ  Eden ์€ ํด๋ฆฌ์–ด๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ unreachable ์˜ค๋ธŒ์ ํŠธ๋“ค๋„ ๊ฐ™์ด ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.

    2. ๊ธฐ์กด S0 ์— ์žˆ์—ˆ๋˜ reachable ์˜ค๋ธŒ์ ํŠธ๋“ค์€ S1 ์œผ๋กœ ์˜ฎ๊ฒจ์ง€๊ณ , ์ด๋•Œ age ๊ฐ’์ด ์ฆ๊ฐ€๋˜์–ด ์˜ฎ๊ฒจ์ง‘๋‹ˆ๋‹ค.

    3. Young Generation ์—์„œ ๊ณ„์†ํ•ด์„œ ์‚ด์•„๋‚จ์•„ age ๊ฐ’์ด ํŠน์ • ๊ฐ’ ์ด์ƒ๋œ ์˜ค๋ธŒ์ ํŠธ๋Š” Old Generation ์œผ๋กœ ์˜ฎ๊ฒจ์ง‘๋‹ˆ๋‹ค. (Promotion)

  3. Promotion ์ž‘์—…์ด ๊ณ„์†๋˜์„œ Old Generation ์ด ๊ฐ€๋“์ฐจ๋ฉด MajorGC ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

Garbage Collector ์ข…๋ฅ˜

Serial GC -XX:+UseSerialGC

  • Java SE 5, 6 ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋””ํดํŠธ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ

  • ์ ์€ ๋ฉ”๋ชจ๋ฆฌ์™€ CPU ์ฝ”์–ด ๊ฐœ์ˆ˜๊ฐ€ ์ ์„ ๋•Œ ์ ํ•ฉํ•œ ๋ฐฉ์‹

  • MinorGC, MajorGC ๋ชจ๋‘ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹œํ–‰๋˜๋ฉฐ Mark-Compact collection method ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • Mark-Compact collection method: ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ๋“ค์„ heap ์˜ ์‹œ์ž‘ ์œ„์น˜๋กœ ์˜ฎ๊ฒจ ๋†“๋Š” ๋ฐฉ๋ฒ•

Parallel GC (Throughput GC) -XX:+UseParallelGC

  • ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ์ˆ˜ํ–‰์‹œ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ (๊ธฐ๋ณธ Young Generation)

  • ๋‹จ ํ˜ธ์ŠคํŠธ ๋จธ์‹ ์ด ์‹ฑ๊ธ€ CPU ๋ผ๋ฉด ๋””ํดํŠธ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ (Serial GC) ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

  • -XX:+UseParallelOldGC ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด, Old Generation ์˜ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์—์„œ๋„ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • JDK 5 update 6 ๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

CMS Collector (Concurrent Mark Sweep Collector, Low Latency GC) -XX:+UseConcMarkSweepGC

  • ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ์ž‘์—…์„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์Šค๋ ˆ๋“œ์™€ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•จ์œผ๋กœ์จ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์œผ๋กœ ์ธํ•œ stop the world ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ

  • Young Generation ์— ๋Œ€ํ•œ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์‹œ Parallel GC ์™€ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, -XX:ParallelCMSThreads={n} ์˜ต์…˜์œผ๋กœ ์Šค๋ ˆ๋“œ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋‹จ์ 

    • ๋ฉ”๋ชจ๋ฆฌ์™€ CPU ์‚ฌ์šฉ๋Ÿ‰์ด ๋†’์Šต๋‹ˆ๋‹ค.

    • ์ผ๋ฐ˜์ ์œผ๋กœ compaction ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์Œ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ํŒŒํŽธํ™”์˜ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. (๋” ํฐ ํž™์‚ฌ์ด์ฆˆ ํ• ๋‹น)

G1 Garbage Collector -XX:+UseG1GC

  • ๋ฐœ์ „ํ•˜๋Š” ํ•˜๋“œ์›จ์–ด ์ŠคํŽ™์— ๋”ฐ๋ผ ์ด์ „์˜ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๊ฐ€ ์ ํ•ฉํ•˜์ง€ ์•Š์•„ ๋‚˜์˜จ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ

  • ํฐ heap ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์งง์€ GC ์‹œ๊ฐ„์„ ๋ณด์žฅํ•˜๋Š”๋ฐ ๋ชฉ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๊ธฐ์กด์˜ ๊ณ ์ •๋œ ํฌ๊ธฐ, ์œ„์น˜๊ฐ€ ์•„๋‹Œ ์ „์ฒด Heap ์˜์—ญ์„ Region ์ด๋ผ๋Š” ํŠน์ •ํ•œ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ  ๋™์ ์œผ๋กœ ์—ญํ• ์„ (Eden, Survivor, Old) ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

Last updated