toss-slash-22

Java Native Memeory Leak ์›์ธ์„ ์ฐพ์•„์„œ

  • system.log์—์„œ ๋ฐœ์ƒํ•˜๋Š” OOM Killer ์„ ์Šฌ๋ž™์œผ๋กœ ์—ฐ๋™

  • OOM Killer?

    • ์ „์ฒด ์‹œ์Šคํ…œ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด, ํฌ์ƒํ•  ์ž‘์—… (ํ”„๋กœ์„ธ์Šค) ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ.

    • ์ผ๋ฐ˜์ ์œผ๋กœ Java๋กœ ์„œ๋น„์Šคํ•˜๊ณ  ์žˆ์œผ๋ฉด, OS์—์„œ Java Process๋ฅผ ์ฃฝ์ด๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ

  • Java Max Heap Size ๊ฐ€ 1.5GB์ธ๋ฐ.. Java ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‚ฌ์šฉํ•œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ 4GB

    • 2.5GB๋Š” ์–ด๋””์„œ ์‚ฌ์šฉ๋œ๊ฒƒ?

์‹ค์ œ ์‚ฌ์šฉ๋Ÿ‰ ์ธก๋ฉ

- `jcmd` ๋ฅผ ํ†ตํ•ด ์ž๋ฐ” ๊ฐ€์ƒ๋จธ์‹ ์˜ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰(heap, thread, code, gc..) ์„ ์•Œ ์ˆ˜ ์žˆ์Œ. (2GB)
- TOP ๋ช…๋ น์–ด์˜ RSS ์ˆ˜์น˜๊ฐ€, ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ OS๋กœ ๋ถ€ํ„ฐ ํ• ๋‹น ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ฆฌ (3.1GB)
- 1.1GB ์˜ค์ฐจ
- ๋ฌธ์ œ๊ฐ€ ์ผ์–ด๋‚ฌ์„ ๋•Œ TOP์€ 4GB๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์—ˆ์„ ๋“ฏ.
- ๊ทธ๋Ÿฌ๋ฉด ์•ฝ 2GB์˜ ์˜ค์ฐจ๊ฐ€ ์ƒ๊ธฐ๊ณ  ์ด๊ฒŒ ์–ด๋””์„œ ๋ฐœ์ƒํ•œ ๊ฑด์ง€ ์•Œ์•„์•ผํ•จ.

Native Memeory Leak

- TOP์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ, `jcmd` ๋ฅผ ํ†ตํ•ด ์ธก์ •๋˜๋Š” ์ž๋ฐ” ๊ฐ€์ƒ๋จธ์‹  ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์˜ ์ฐจ์ด
- ์ด ์ปจํผ๋Ÿฐ์Šค์—์„œ๋Š” ์•ฝ 2GB ๋ผ ํ•จ (4GB - 2GB)

์˜ˆ์ƒ ์›์ธ

- JNI & JNA
- DirectBuffer (native memory ๊ฐ€ ํ• ๋‹น๋จ. ๊ทธ๋Ÿฌ๋ฉด GC์—์„œ ์ •๋ฆฌํ•˜์ง€ ๋ชปํ•จ)
- APM ํˆด (JavaAgent ๊ธฐ๋ฐ˜)
- ์œ„ ๋ฆฌ์ŠคํŠธ์—์„œ ์ฐพ์ง€ ๋ชปํ•จ

์‹œ๋„ํ–ˆ๋˜ ๋ฐฉ๋ฒ•

- ํ”„๋กœ์„ธ์Šค ์กฐ์‚ฌ, ํ”„๋กœ์„ธ์Šค ๋ฉ”๋ชจ๋ฆฌ dump (pmap, smaps, gdb) strings ๋ฅผ ์ด์šฉ
- Memory Allocator (malloc -> jemalloc)
    - ์–ด๋–ค ๋ชจ๋“ˆ์—์„œ ์–ผ๋งˆ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Œ.
    - C2 Complier
  • C2 Complier

    • Java ํŒŒ์ผ ์ปดํŒŒ์ผ ์‹œ class ํŒŒ์ผ ์ƒ๊น€

    • classํŒŒ์ผ์„ OS๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๊ณ„์–ด๋กœ ์ปดํŒŒ์ผํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด ์—ญํ• ์„ JIT Complier ๊ฐ€ ํ•˜๊ฒŒ๋จ.

    • JIT Complier์˜ ๋ ˆ๋ฒจ์€ 0~4 ๋‹จ๊ณ„๋กœ ์ง„ํ–‰ํ•˜๋Š”๋ฐ, ์ด ์ค‘ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„ ํ•ด๋‹น ํ•˜๋Š” ๊ฒƒ์ด C2 Complier

    • C1 Complier๋Š” ๋น ๋ฅด๊ฒŒ ์ปดํŒŒ์ผ๋˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ (Client Complier)

    • C2 ๋Š” Server Complier๋กœ, ๊ตฌ๋™์‹œ๊ฐ„์€ ๋Š๋ฆฌ์ง€๋งŒ, JIT Complier์˜ ์ตœ์ ํ™”๋ฅผ ๋งŽ์ดํ•˜์—ฌ์„œ ๋น ๋ฅด๊ฒŒ ์—ฐ์‚ฐํ•  ๋•Œ ์‚ฌ์šฉ๋จ.

      • ๊ทธ๋ž˜์„œ ์„œ๋ฒ„์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•จ

  • TiredStopAtLevel์ด๋ผ๋Š” ์˜ต์…˜์„ ํ†ตํ•ด Java์—์„œ๋Š” Complier ๋ ˆ๋ฒจ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ.

ํ•ด๊ฒฐ

1. C1 Complier๋กœ ๋ณ€๊ฒฝ. ํ•ด๊ฒฐ ๊ทธ๋ ‡์ง€๋งŒ CPU ์‚ฌ์šฉ๋Ÿ‰์ด 2๋ฐฐ ์ฆ๊ฐ€
2. JDK 11, 17 ๋ณ€๊ฒฝ, ํ•ด๊ฒฐ ์•ˆ๋จ.
3. OpenJDK์˜ Graal Complier ์‚ฌ์šฉ

์ฐธ๊ณ 

  • https://www.youtube.com/watch?v=w4fWgLgop5U


ํ† ์Šค์ฆ๊ถŒ ์‹ค์‹œ๊ฐ„ ์‹œ์„ธ ์ ์šฉ๊ธฐ

  • WebSocket

    • ๋น„์ •์ƒ์ ์ด๋ฉด, REST API ์‚ฌ์šฉ (Polling API)

    • ์‹ค์‹œ๊ฐ„ ์‹œ์„ธ, ์—ญ๋™์ !

  • ๋ง๋ถ„๋ฆฌ ์š”๊ฑด์„ ๊ณ ๋ คํ•œ ์ธํ”„๋ผ ๊ตฌ์กฐ

    • ๋ง๋ถ„๋ฆฌ ์š”๊ฑด

      • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฆ๊ถŒ์‚ฌ ์„œ๋ฒ„์— ์ง์ ‘ ์ ‘์†ํ•ด์„  ์•ˆ๋˜๊ณ , ์›น์„œ๋ฒ„(DMZ) ๋ฅผ ๊ฒฝ์œ ํ•ด์•ผํ•จ.

      • ํ† ์Šค์—์„œ๋Š” WebSocket ์„œ๋ฒ„๊ฐ€ DMZ

  • Spring WebSocket with Stomp

Load Balancing ๋ฐฉ์‹์— ๋”ฐ๋ฅธ Connection ์ง€์—ฐ

  • ํ•ด๊ฒฐ

    • Round Robin + Least conntion

WebSocket Connection Leak

  • ๋ฌธ์ œ

    • ์ ‘์†์‚ฌ ์ˆ˜ ์ฐจ์ด (HTTP vs WEBSOCKET, ๊ฐ™์•„์•ผํ•˜๋Š”๋ฐ..)

    • abnoraml ์ข…๋ฃŒ ์‹œ, ์ œ๋Œ€๋กœ connection ์ด ์ œ๋Œ€๋กœ ์ข…๋ฃŒ๋˜์ง€ ์•Š์Œ.

  • ํ•ด๊ฒฐ

    • WebSocketHandler๋ฅผ ์ƒˆ๋กœ ๊ตฌํ˜„ํ•˜์—ฌ์„œ, ๋ช…์‹œ์ ์ธ ์ผ€์ด์Šค (1001..) ์— WebSocket Conenction์ด ๋‹ซํžˆ๋„๋ก ๊ตฌํ˜„

๋ณด์•ˆ ์žฅ๋น„ ์ด์Šˆ

  • ์‹ค์‹œ๊ฐ„ ์„œ๋น„์Šค ํŠธ๋ž˜ํ”ฝ์ด ๋Š˜๋ฉด์„œ, ๋ฐฉํ™”๋ฒฝ์—์„œ ๋ ˆ์ดํ„ด์‹œ ๋ฐœ์ƒ

  • ์ด ๋ ˆ์ดํ„ด์‹œ๋กœ ์ธํ•ด, ํ•ด๋‹น ๋ณด์•ˆ ์ธํ”„๋ผ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ ์„œ๋น„์Šค์—์„œ๋„ ์—๋Ÿฌ๊ฐ€ ์ „ํŒŒ๋จ.

Scale out ์–ด๋ ค์›€

  • AWS Cloud๋กœ ์ด์ „ ๊ณ ๋ ค์ค‘

์ฐธ๊ณ 

  • https://www.youtube.com/watch?v=WKYE-QtzO6g&list=PL1DJtS1Hv1PiGXmgruP1_gM2TSvQiOsFL&index=11

์™œ ์€ํ–‰์€ ๋ฌดํ•œ์Šคํฌ๋กค์ด ์•ˆ๋˜๋‚˜์š”

  • ์™œ?

    • ์ฝ”์–ด๋ฑ…ํ‚น ์„œ๋ฒ„ (๊ณ„์ •๊ณ„) ์— ์ ์€ ๋ถ€ํ•˜๋ฅผ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ํŠน์ • ์ผ์ž๋กœ ์ œํ•œ ํ•˜๋Š” ๊ฒƒ.

  • ํ† ์Šค ๋ฑ…ํฌ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์š”์ฒญ์— ๋Œ€ํ•ด ์ฝ”์–ด ๋ฑ…ํ‚น์— ๋ฐ”๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด์ง€ ์•Š์Œ.

์ฐธ๊ณ 

  • https://www.youtube.com/watch?v=v9rcKpUZw4o&list=PL1DJtS1Hv1PiGXmgruP1_gM2TSvQiOsFL&index=12

Last updated