[Java] String Pool
Java์ String Pool์ JVM์ด ๋ฌธ์์ด ๊ฐ์ฒด๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ํน๋ณํ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ด๋ค. ์ด๋ฅผ ํตํด ๋์ผํ ๋ฌธ์์ด ๊ฐ์ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ์ฌ์ฌ์ฉํ์ฌ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ค์ด๊ณ ์ฑ๋ฅ์ ํฅ์์ํฌ ์ ์๋ค.
๊ธฐ๋ณธ ๊ฐ๋
String Pool(๋๋ String Constant Pool, String Intern Pool)์ JVM์ ํ ๋ฉ๋ชจ๋ฆฌ ๋ด์ ์กด์ฌํ๋ ํน๋ณํ ๊ณต๊ฐ์ผ๋ก, ๋ฌธ์์ด ๋ฆฌํฐ๋ด์ด ์ ์ฅ๋๋ค. String Pool์ ๋ฌธ์์ด์ ๋ถ๋ณ์ฑ(immutability)๊ณผ String interning์ด๋ผ๋ ๊ฐ๋ ์ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ค.
2. ๋ฌธ์์ด ์์ฑ ๋ฐฉ์๊ณผ String Pool
1. ๋ฌธ์์ด ๋ฆฌํฐ๋ด ์์ฑ
String s1 = "hello";
String s2 = "hello";
- JVM์ str1์ ์์ฑํ ๋ "hello"๋ผ๋ ๋ฆฌํฐ๋ด์ด String Pool์ ์กด์ฌํ๋์ง ํ์ธํ๋ค.
- ๋ง์ฝ ์กด์ฌํ๋ค๋ฉด, ํด๋น ๊ฐ์ฒด์ ์ฐธ์กฐ๋ฅผ ๋ฐํํ๊ณ , ์กด์ฌํ์ง ์๋๋ค๋ฉด ์๋ก์ด ๋ฌธ์์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ String Pool์ ์ ์ฅํ ํ ์ฐธ์กฐ๋ฅผ ๋ฐํํ๋ค.
- ๋ ๋ฒ์งธ "hello"๋ฅผ ๋ง๋ ๋๋ Pool์์ ๊ธฐ์กด ๋ฌธ์์ด์ ์ฐพ์ ์ฐธ์กฐ๋ง ๋ฐํํ๋ค.
- ๊ฒฐ๊ณผ์ ์ผ๋ก s1 == s2๋ true๊ฐ ๋๋ค. (๊ฐ์ ๊ฐ์ฒด ์ฐธ์กฐ)
2. new ํค์๋ ๋ฐฉ์
String s3 = new String("hello");
- new ํค์๋ ์ฌ์ฉ ์ ํ ๋ฉ๋ชจ๋ฆฌ์ ์ ๋ฌธ์์ด ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค.
- ์ด ๊ฒฝ์ฐ ๋์ผํ ๊ฐ์ ๋ฌธ์์ด์ด String Pool์ ์กด์ฌํ๋๋ผ๋ ์ด๋ฅผ ๋ฌด์ํ๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
- ์ฆ, String Pool์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฌ์ฉํ์ง ์๋๋ค.
- ๋ฐ๋ผ์ s1 == s3๋ false์ด๋ค. (๋ค๋ฅธ ๊ฐ์ฒด ์ฐธ์กฐ)
3. intern() ๋ฉ์๋(String Interning)
String interning์ ๋์ผํ ๊ฐ์ ๊ฐ์ง ๋ฌธ์์ด ๊ฐ์ฒด๋ฅผ ํ๋๋ง ์ ์งํ๋๋ก ํ๋ JVM์ ์ต์ ํ ๊ธฐ๋ฒ์ด๋ค.
String s4 = new String("hello").intern();
- intern()์ String Pool์์ ๋์ผํ ๋ด์ฉ์ ๋ฌธ์์ด์ ์ฐพ๋๋ค.
- ์์ผ๋ฉด ํด๋น ์ฐธ์กฐ๋ฅผ ๋ฐํํ๊ณ , ์์ผ๋ฉด Pool์ ์ถ๊ฐ ํ ๋ฐํํ๋ค.
- s1 == s4๋ true๊ฐ ๋๋ค.
JVM์์ ๋ ์ด์ ์ฌ์ฉ๋์ง ์๋ ๋ฌธ์์ด์ ๊ฐ๋น์ง ์ปฌ๋ ์ ๋์์ด ๋ ์ ์์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก String Pool ๋ด์ ๋ฆฌํฐ๋ด์ ํ๋ก๊ทธ๋จ ์ข ๋ฃ ์๊น์ง ์ ์ง๋๋ค. ๋ฐ๋ผ์ String Pool์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ต์ ํํ์ง๋ง, ๊ณผ๋ํ intern ์ฌ์ฉ์ ์คํ๋ ค ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ฆ๊ฐ์ํฌ ์ ์์ด ์ด๋ฅผ ์ฃผ์ํด์ผ ํ๋ค.
4. ๋์ ๊ณผ์
- ๋ฌธ์์ด ๋ฆฌํฐ๋ด์ด ๋ฑ์ฅํ๋ฉด JVM์ ๋ด๋ถ์ ์ผ๋ก String Pool์์ ํด๋น ๋ฌธ์์ด์ ๊ฒ์ํ๋ค.
- ๊ฒ์์๋ ํด์ ํ ์ด๋ธ์ด ์ฌ์ฉ๋๋ค. (๋ฌธ์์ด์ ํด์์ฝ๋ ๊ธฐ๋ฐ)
- ์ผ์นํ๋ ๋ฌธ์์ด์ด ์์ผ๋ฉด ๊ธฐ์กด ์ฐธ์กฐ๋ฅผ ๋ฐํํ๋ค.
- ์ผ์นํ๋ ๋ฌธ์์ด์ด ์์ผ๋ฉด String Pool์ ํด๋น ๋ฌธ์์ด์ ์๋ก ์ถ๊ฐํ๊ณ ๊ทธ ์ฐธ์กฐ๋ฅผ ๋ฐํํ๋ค.
String ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ ๋ฐฉ์ ๋ณํ
- Java 7 ์ด์ : String Pool์ PermGen ์์ญ(๊ณ ์ ํฌ๊ธฐ)์ ์์๋ค.
- Java 7 ์ดํ: Heap ์์ญ์ผ๋ก ์ด๋ํ์ฌ GC ๋์์ด ๋์๋ค.
- Java 9 ์ดํ: Compact Strings ๋์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ์ด ๋์ฑ ํฅ์๋์๋ค.
์ฐธ๊ณ ์๋ฃ:
https://en.wikipedia.org/wiki/String_interning
String interning - Wikipedia
From Wikipedia, the free encyclopedia Data structure for reusing strings In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable.[1] Interning strings makes some string processing ta
en.wikipedia.org
https://www.scaler.com/topics/java/string-pool-in-java/
String Pool in Java - Scaler Topics
Learn about String Pool in Java by Scaler Topics. String Pool or String Constant Pool is a special area in Java Heap memory where string literals are stored.
www.scaler.com
https://www.digitalocean.com/community/tutorials/what-is-java-string-pool
What is Java String Pool? | DigitalOcean
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
www.digitalocean.com
https://www.baeldung.com/java-9-compact-string