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.)