Java Memory(Runtime Data Area) - GANGNAM-JAVA/JAVA-STUDY GitHub Wiki

Runtime Data Area

Runtime Data Area๋Š” JVM์ด ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด OS๋กœ๋ถ€ํ„ฐ ํ• ๋‹น๋ฐ›๋Š” ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์ด๋‹ค.

WAS์˜ ์„ฑ๋Šฅ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ, ๋Œ€๋ถ€๋ถ„ ์ด ์˜์—ญ๋“ค์ด ์›์ธ์ด ๋œ๋‹ค.(Memory Leak ํ˜น์€ GC)

Runtime Data Area๋Š” 5๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

  • PC Register
  • JVM stack
  • Native Method stack
  • Heap
  • Method Area

์œ„ ๊ทธ๋ฆผ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค์‹œํ”ผ, ์œ„ 3๊ฐœ์˜ ์˜์—ญ์€ Thread๋ณ„๋กœ ์ƒ์„ฑ๋˜๊ณ , ์•„๋ž˜ 2๊ฐœ์˜ ์˜์—ญ์€

๋ชจ๋“  Thread๊ฐ€ ๊ณต์œ ํ•œ๋‹ค.

PC Register (Thread๋ณ„๋กœ 1๊ฐœ์”ฉ ์กด์žฌ)

Java์˜ PC Register๋Š” CPU ๋‚ด์˜ ๊ธฐ์–ต์žฅ์น˜์ธ ๋ ˆ์ง€์Šคํ„ฐ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•œ๋‹ค.

(Register-Base๊ฐ€ ์•„๋‹Œ Stack-base๋กœ ์ž‘๋™)

ํ˜„์žฌ ์ˆ˜ํ–‰ ์ค‘์ธ JVM Instruction ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค

JVM stack (Thread๋ณ„๋กœ 1๊ฐœ์”ฉ ์กด์žฌ)

Thread์˜ Method๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ˆ˜ํ–‰ ์ •๋ณด(๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์ฃผ์†Œ, ๋งค๊ฐœ ๋ณ€์ˆ˜, ์ง€์—ญ ๋ณ€์ˆ˜, ์—ฐ์‚ฐ ์Šคํƒ)๊ฐ€

Frame ์ด๋ผ๋Š” ๋‹จ์œ„๋กœ JVM stack์— ์ €์žฅ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Method ํ˜ธ์ถœ์ด ์ข…๋ฃŒ๋  ๋•Œ stack์—์„œ ์ œ๊ฑฐ๋œ๋‹ค.

Native Method stack (Thread๋ณ„๋กœ 1๊ฐœ์”ฉ ์กด์žฌ)

Java ์™ธ์˜ ์–ธ์–ด๋กœ ์ž‘์„ฑ๋œ ๋„ค์ดํ‹ฐ๋ธŒ ์ฝ”๋“œ๋“ค์„ ์œ„ํ•œ stack

(ex JNI๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœ๋˜๋Š” C/C++ ๋“ฑ์˜ ์ฝ”๋“œ)

Heap (๋ชจ๋“  Thread๊ฐ€ ๊ณต์œ )

์ธ์Šคํ„ด์Šค์™€ ๋ฐฐ์—ด์ด ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ณต๊ฐ„.

๊ทธ๋ฆฌ๊ณ  Garbage Collection์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ์˜์—ญ.

๊ฐœ๋ฐœ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ๋ณ„๋„์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๊ณ  ์˜คํžˆ๋ ค ๋ถ€์ž‘์šฉ๋งŒ ๋‚ณ์„ ๊ฐ€๋Šฅ์„ฑ์ด ํผ.

๋ชจ๋“  Thread๊ฐ€ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋™๊ธฐํ™” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

Method Area (๋ชจ๋“  Thread๊ฐ€ ๊ณต์œ )

Class Loader๊ฐ€ ์ ์žฌํ•œ ํด๋ž˜์Šค(๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค)์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ •๋ณด๊ฐ€ ์ €์žฅ๋œ๋‹ค.

์ด ์˜์—ญ์— ๋“ฑ๋ก๋œ class๋งŒ์ด Heap์— ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์‹ค Method Area๋Š” ๋…ผ๋ฆฌ์ ์œผ๋กœ Heap์— ํฌํ•จ๋œ๋‹ค.

๋” ๊ตฌ์ œ์ ์œผ๋กœ๋Š” Heap์˜ PermGen์ด๋ผ๋Š” ์˜์—ญ์— ์†ํ•œ ์˜์—ญ์ธ๋ฐ, Java 8 ์ดํ›„๋กœ๋Š”

Metaspace๋ผ๋Š” OS๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์˜์—ญ์œผ๋กœ ์˜ฎ๊ฒจ์ง€๊ฒŒ ๋œ๋‹ค.

Type Information
- Type(class or interface)์˜ ์ „์ฒด ์ด๋ฆ„
- Type์˜ ์ง๊ณ„ ํ•˜์œ„ ํด๋ž˜์Šค ์ „์ฒด ์ด๋ฆ„
- Type์˜ class/interface ์—ฌ๋ถ€
- Type์˜ modifier (public / abstract / final)
- ์—ฐ๊ด€๋œ interface ์ด๋ฆ„ ๋ฆฌ์ŠคํŠธ

Field Information
- Field Type
- Field Modifier (public / private / protected / static / final / volatile / transient)

Method Information
- Constructor๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  Method์˜ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

Runtime Constant Pool
- Type, Field, Method๋กœ์˜ ๋ชจ๋“  ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ €์žฅ
- JVM์€ ์ด ์˜์—ญ์„ ํ†ตํ•ด ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์˜ ์ฃผ์†Œ๋ฅผ ์ฐพ์•„ ์ฐธ์กฐ

Class Variable
- static ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ์ €์žฅ
- ๊ธฐ๋ณธํ˜•์ด ์•„๋‹Œ static ๋ณ€์ˆ˜๋Š” ๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜๋งŒ ์ €์žฅ๋˜๊ณ  ์‹ค์ œ ์ธ์Šคํ„ด์Šค๋Š” Heap์— ์ €์žฅ๋จ
- ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ด์ „์— ์ด ๋ณ€์ˆ˜๋“ค์€ ๋ฏธ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›์Œ

์งˆ๋ฌธ 1 :: Java heap memory ์ฃผ์†Œ๊ฐ’์œผ๋กœ ์ธ์Šคํ„ด์Šค์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ• ๊นŒ?

๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐœ๋ฐœ์ž๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค.

์˜ค์ง ๊ฐ„์ ‘์ ์œผ๋กœ๋งŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด, String str = "hello"; ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด "hello" ๋ฌธ์ž์—ด์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€

str์— ํ• ๋‹น๋˜๊ณ  ์žˆ๋‹ค. ์ด์ œ str์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  String str2 = str; ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด str์ด ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์ฃผ์†Œ๊ฐ€ str2์—

ํ• ๋‹น๋˜๊ณ  ์žˆ๋‹ค. "hello"๋ผ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ "hello"์˜ ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ

ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐ ๋ณ€์ˆ˜์— ์ง์ ‘ ํ• ๋‹นํ•  ์ˆ˜๋Š” ์—†๋‹ค.

์™œ๋ƒํ•˜๋ฉด ์‹ค์ œ ์ฃผ์†Œ๋ฅผ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. JVM๋งŒ์ด ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์œ„์น˜๋ฅผ

์•Œ๊ณ  ์žˆ์œผ๋ฉฐ, ํ• ๋‹น ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ๋™์•ˆ ํ•ด๋‹น ์ฃผ์†Œ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.

์ฐธ์กฐ ๋ณ€์ˆ˜์— ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผ์†Œ๋Š” null ๋ฟ์ด๋‹ค.

์›๋ฌธ

You cannot assign the address of an object directly. You can only do so indirectly.
For example, in statement the 'String str = "hello";' you are assigning the address of 
the memory location at which the string "hello" is stores to str variable.
str, therefore, now contains the address of a memory location.

Similarly, in statement 'String str2 = str;' you are assigning the value stored in str 
to str2. You are not copying "hello" to str2. You are just copying the address stored 
in str to str2.

You cannot assign a memory address to a reference variable directly because you don't know 
the actual address. Only the JVM knows where an object is stored in memory and it assigns 
that address to the variable while executing the assignment operation. The only "address" 
you can assign to a reference variable directly is null.

์งˆ๋ฌธ 2 :: ๊ทธ๋Ÿฌ๋ฉด ์™œ JVM๋งŒ์ด ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์œ„์น˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ?

์ž๋ฐ”์—์„  ๋ณ€์ˆ˜์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์–ป๋Š” ๊ฒƒ์€ ๋ฌด์˜๋ฏธํ•˜๋‹ค.

์™œ๋ƒํ•˜๋ฉด JVM์€ ์ž์œ ๋กญ๊ฒŒ ๊ฐ์ฒด๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ , GC๋ฅผ ์ด์šฉํ•ด ๊ทธ ๊ฐ์ฒด์˜ ์œ„์น˜๋ฅผ ์ด๋™์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

JVM๋งŒ์ด ์ •ํ™•ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์•Œ๊ณ ์žˆ๋‹ค.

์›๋ฌธ

Getting the memory addresses of variables is meaningless within Java, since the JVM is 
at liberty to implement objects and move them as it seems fit 
(your objects may/will move around during garbage collection etc.)