Languege/Java & Spring / / 2024. 1. 7. 18:00

[String Constant Pool] Heap? PermGen? ์˜คํ•ด์™€ ์ง„์‹ค

๐ŸŽˆ ๊ฐœ์š”

๊ธฐ์ˆ  ๋ฉด์ ‘์„ ์ค€๋น„ํ•˜๊ณ  ๋ณด๋˜ ๋„์ค‘, ํ•œ ๋ฉด์ ‘๊ด€๋‹˜๊ป˜์„œ ์ด๋Ÿฐ ์งˆ๋ฌธ์„ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.

Q.๋ฌธ์ž์—ด์„ String str = "hello"; ๋ผ๊ณ  ์„ ์–ธํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?

A.JVM ๋ฉ”๋ชจ๋ฆฌ ๋‚ด์˜ Heap ์˜์—ญ์— ์žˆ๋Š” String Constant Pool์— ์ €์žฅ์ด ๋ฉ๋‹ˆ๋‹ค.

Q.String Constant Pool์ด Heap์˜์—ญ์— ์ €์žฅ๋˜๋Š”๊ฒŒ ๋งž์•„์š”??

์ €๋Š” ๋‹น์—ฐํžˆ Heap ์˜์—ญ ๋‚ด๋ถ€์— ์ €์žฅ๋˜๋Š”๊ฑฐ ์•„๋‹ˆ์•ผ? ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.(๋ธ”๋กœ๊ทธ๋ฅผ ํ†ตํ•ด์„œ ํ•™์Šต ํ•˜์‹ ๋ถ„์€ ๋ฐ˜์€ ๋งž๋‹ค. ๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹ค ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์•„์š”, ๋ณดํ†ต Heap์•ˆ์— PermGen์•ˆ์— String Constant Pool์ด ์žˆ๋‹ค๊ณ  ์„ค๋ช…ํ•จ)

 

์ •ํ™•ํžˆ, JAVA 7๊นŒ์ง€๋Š” Permgen์— ์ €์žฅ๋˜๊ณ  JAVA 8 ์ดํ›„๋ถ€ํ„ฐ๋Š” NativeMemory ๋‚ด๋ถ€์˜ Metaspace์— ์ €์žฅ์ด ๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด์„œ ๊ณ„์† ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๋ณด๋‹ค ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทธ๋ฆผ์„ ๋ฐœ๊ฒฌํ•ฉ๋‹ˆ๋‹ค.

JVM Memory Architecture๋กœ ํ”ํžˆ ๋‚˜์˜ค๋Š” ์žฅํ‘œ(๋ญ๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ?)

 

โ“ ๊ทธ๋Ÿผ JVM Memory์— Heap ์˜์—ญ์— Permanent Generation์ด ์žˆ๋Š”๊ฒŒ ๋งž์ž–์•„?

์œ„์˜ ๊ทธ๋ฆผ์€ JVM Memory Architecture ๊ฒ€์ƒ‰ํ•˜๋ฉด ํ”ํ•˜๊ฒŒ ๋‚˜์˜ค๊ฑฐ๋‚˜, ๊ตญ๋‚ด ๋ธ”๋กœ๊ทธ๋‚˜ ํฌ์ŠคํŒ…๋“ค์„ ๋ณด๋ฉด ์œ„์™€ ๊ฐ™์ด Heap ์˜์—ญ์•ˆ์— Permgen์ด ์žˆ๊ณ , Permgen ๋‚ด๋ถ€์— String ConstantPool์ด ์žˆ๋‹ค๊ณ  ํ•˜๋Š” ๊ทธ๋ฆผ์„ ๋งŽ์ด ๋ณด์…จ์„๊ฒ๋‹ˆ๋‹ค.

์ž˜๋ชป๋œ ๊ทธ๋ฆผ

new String()์„ ํ†ตํ•ด ์ƒ์„ฑํ•˜๋ฉด Heap ์˜์—ญ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋˜๋‹ˆ๊นŒ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋กœ ์ธํ•ด ์ ˆ๋Œ€ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.

๋งž๋Š” ๋ง ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด ๊ทธ๋ฆผ์€ ์ž˜๋ชป๋์Šต๋‹ˆ๋‹ค.

 

๐Ÿคฆ‍โ™‚๏ธ Heap์™€ PermGen์€ ๋‹ค๋ฅธ ์˜์—ญ์ด๋‹ค.

์ •ํ™•ํ•˜๊ฒŒ Memory๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ทธ๋ฆผ

์œ„์™€ ๊ฐ™์ด PermGen์€ Heap์˜์—ญ์— ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Java๋Š” Heap Memory์™€ Non-Heap Memory ๋‘ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. Oracle GC ๊ฐœ๋ฐœ์ž ๋ธ”๋กœ๊ทธ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์žฅํ‘œ๋ฅผ ํ†ตํ•ด์„œ Permanent Generation ์˜์—ญ์€ ํž™ ์˜์—ญ์ด ์•„๋‹Œ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋กœ ํฌ์ŠคํŒ…์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

GC ๊ฐœ๋ฐœ์ž ๋ธ”๋กœ๊ทธ

 

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— Heap Memory OOM๊ณผ Non-Heap Memory OOM์€ ์„œ๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

 

โค๏ธ ๊ฒฐ๋ก 

1. String Constant Pool์€ Java 7๊นŒ์ง€๋Š” Perm ์˜์—ญ์— ๊ด€๋ฆฌ๋œ๋‹ค. ์ดํ›„์—๋Š” Native Method ๋‚ด๋ถ€ MetaSpace์—์„œ ๊ด€๋ฆฌ

2. Perm ์˜์—ญ์€ Heap ์˜์—ญ๊ณผ ๋‹ค๋ฅธ Non-Heap Memory๋‹ค.

3. Permanent Generation์€ OOM๋„ Heap ์˜์—ญ๊ณผ ๋‹ค๋ฅด๋‹ค.

4. ์ด์ œ ๋ˆ„๊ฐ€ String Constant Pool์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด๋‹ฌ๋ผ๊ณ  ํ•˜๋ฉด, JVM Memory ๋‚ด์— ์œ„์น˜ํ•œ Non-Heap Memory์ธ PermGen ์˜์—ญ์— String Constant Pool์ด ์œ„์น˜ํ•œ๋‹ค๊ณ  ๋งํ•ด์ฃผ์ž. 

 

[reference]

https://deveric.tistory.com/123

https://blog.naver.com/simjunbo/221080829920

https://stackoverflow.com/questions/41358895/permgen-is-part-of-heap-or-not

https://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html

๊ทธ ์™ธ ๊ตญ๋‚ด ๋ธ”๋กœ๊ทธ

  • ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ๊ณต์œ 
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ ๊ณต์œ 
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ 
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ