# [Java] String Constant pool๊ณผ Heap ์˜์—ญ์— ๋Œ€ํ•ด
Study Repository

[Java] String Constant pool๊ณผ Heap ์˜์—ญ์— ๋Œ€ํ•ด

by rlaehddnd0422
public class Person {

    private String name = "KIM";

    public String getName() {
        return name;
    }

    private void introduce() {
        System.out.println("My Name is " + this.name);
    }
}

String์˜ ๋ถˆ๋ณ€์„ฑ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์˜๋ฌธ์ ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
 
๋งŒ์•ฝ ์œ„์˜ ํด๋ž˜์Šค๋กœ ์ธ์Šคํ„ด์Šค๊ฐ€ 100๊ฐœ ์ƒ์„ฑ๋œ๋‹ค๋ฉด ๋ฉค๋ฒ„๋ณ€์ˆ˜ name์€ 100๊ฐœ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น ๋ฐ›์„๊นŒ?
์•„๋‹ˆ๋ฉด 100๊ฐœ์˜ ๊ฐ ์ธ์Šคํ„ด์Šค๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜ name์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ์„๊นŒ? 
 
 Person ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” new ํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑ๋˜์–ด Heap ์˜์—ญ์—์„œ ๊ด€๋ฆฌ๋˜๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ๋˜ํ•œ Heap ์˜์—ญ์—์„œ ๊ด€๋ฆฌ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ 100๊ฐœ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›์„ ๊ฒƒ ๊ฐ™์•˜์ง€๋งŒ, ์‹ค์ƒ์€ ๊ทธ๋ ‡์ง€ ์•Š๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด String Constant pool์—์„œ ๊ด€๋ฆฌ๋˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

public class PersonTest {
    public static void main(String[] args) {
        Person p = new Person();
        Person p2 = new Person();
        System.out.println(p.getName().equals(p2.getName())); // ๋™๋“ฑ์„ฑ ๊ฒ€์‚ฌ๊ฒฐ๊ณผ : true ( ๊ฐ’์ด ๊ฐ™์€์ง€ )
        System.out.println(p.getName() == p2.getName()); // ๋™์ผ์„ฑ ๊ฒ€์‚ฌ๊ฒฐ๊ณผ : true ( ์ฃผ์†Œ๊นŒ์ง€ ๊ฐ™์€์ง€ )
    }
}

 
์ด ์˜๋ฌธ์ ์— ๋Œ€ํ•ด ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด String Constant Pool๊ณผ Heap ์˜์—ญ์— ๋Œ€ํ•ด ์ •ํ™•ํžˆ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ JVM์— String ๊ฐ์ฒด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ €์žฅ๋˜๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค.


 

 
์šฐ์„  Java์—์„œ String ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ. ๋ฆฌํ„ฐ๋Ÿด์ธ ์Œ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
    • EX) String str = "HI";
  • ๋‘ ๋ฒˆ์งธ. new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
    • EX) String str = new String("HI");

์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ณด๋ฉด ๊ฐ™์•„ ๋ณด์ด์ง€๋งŒ "์–ด๋Š" ๊ณณ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›์•„ ์ €์žฅํ•˜๋Š๋ƒ์— ๋”ฐ๋ฅธ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
 
๊ฒฐ๋ก ์ ์œผ๋กœ ๋งํ•˜์ž๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ฐฉ์‹(๋ฆฌํ„ฐ๋Ÿด)์€ Heap์˜์—ญ ๋‚ด๋ถ€์˜ String constant pool์ด๋ผ๋Š” ์˜์—ญ์—, ๋‘ ๋ฒˆ์งธ ๋ฐฉ์‹(new)์€ Heap ์˜์—ญ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
 
์ฒซ ๋ฒˆ์งธ ๋ฐฉ์‹๋ถ€ํ„ฐ ์‚ดํŽด๋ด…์‹œ๋‹ค.
 

String Constant Pool

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋ฆฌํ„ฐ๋Ÿด๋กœ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜์—ฌ ๋‘ String ๊ฐ์ฒด ๋ชจ๋‘ ๋‹ค ์ŠคํŠธ๋ง ํ’€์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ ๋™๋“ฑ์„ฑ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋™์ผ์„ฑ ๊นŒ์ง€ ๋ณด์žฅ๋˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๊ฐ€๋Šฅํ•œ ๊ฑธ๊นŒ์š”?

public class ImmutableStringTest3 {
    public static void main(String[] args) {
        String s1 = "HELLO";
        String s2 = "HELLO"; // String s2 = s1;

        System.out.println(s1.equals(s2)); // true
        System.out.println(s1 == s2); // true
    }
}

String pool์— String literal๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ๊ฐ’์ด ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋Š” String pool์˜ reference๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜์–ด ๋™์ผ์„ฑ๊นŒ์ง€ ๋ณด์žฅ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

  1. String s1 = "HELLO" : literal๋กœ ์ƒ์„ฑ๋จ โžก๏ธ String Constant Pool์— ํ•ด๋‹น String ๊ฐ’๊ณผ ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง„ ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ โžก๏ธ ์—†์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น
  2. String s2 = "HELLO" : literal๋กœ ์ƒ์„ฑ๋จ โžก๏ธ String Contant Pool์— ํ•ด๋‹น String ๊ฐ’๊ณผ ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง„ ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ โžก๏ธ ์žˆ์œผ๋ฏ€๋กœ ์ฐธ์กฐ๋ฅผ ํ†ตํ•œ ๋Œ€์ž… (s2 = s1;)

+ Java 7 ๋ฒ„์ „๋ถ€ํ„ฐ String Pool์ด Heap ์˜์—ญ ๋‚ด๋ถ€๋กœ ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด์„œ String pool์—์„œ๋„ GC๊ฐ€ ์ˆ˜ํ–‰๋˜๊ฒŒ ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ ์˜ค๋ฅ˜ ์œ„ํ—˜ ๋˜ํ•œ ์ค„์–ด๋“ค๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด String Constant Pool์€ Runtime์—์„œ ๋งŽ์€ ์–‘์˜ String ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋”๋ผ๋„ ๋งŽ์€ ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Heap ์˜์—ญ 

๋ฐ˜๋ฉด new ํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋Š” Heap ์˜์—ญ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ String pool๊ณผ ๋‹ฌ๋ฆฌ ๊ฐ•์ œ์ ์œผ๋กœ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๊ฐ’์„ ๊ฐ–๋„๋ก ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

public class ImmutableStringTest3 {
    public static void main(String[] args) {
        String s1 = new String("HELLO");
        String s2 = new String("HELLO");
        
        System.out.println(s1.equals(s2)); // true
        System.out.println(s1 == s2); // false
    }
}

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด s1์™€ s2๊ฐ€ Heap ์˜์—ญ์— ์ƒ์„ฑ๋จ์œผ๋กœ์จ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋™๋“ฑ์„ฑ์€ ๊นจ์ง€์ง€ ์•Š์ง€๋งŒ, ๋™์ผ์„ฑ(๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•จ)์ด ๊นจ์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. String s1 = new String("HELLO") : new๋กœ ์ƒ์„ฑ๋จ โžก๏ธ ํž™ ์˜์—ญ์— ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
  2. String s2 = new String("HELLO") : new๋กœ ์ƒ์„ฑ๋จ โžก๏ธ ํž™ ์˜์—ญ์— ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

new ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ ๋ชจ๋“  ๊ฐ์ฒด๋Š” Heap ์˜์—ญ์— ๊ฐ๊ฐ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›๊ฒŒ ๋˜์–ด ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋™๋“ฑ์„ฑ์„ ๋ณด์žฅํ• ์ง€ ๋ชฐ๋ผ๋„, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ์ฒด๊ฐ€ ๊ฐ๊ฐ์˜ Heap ์˜์—ญ์„ ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•˜์ง€ ์•Š๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
 
์ด๋Ÿฌํ•œ ์ ์—์„œ String pool์„ ์ด์šฉํ•˜๋Š๋ƒ, new ํ‚ค์›Œ๋“œ๋กœ Heap์˜์—ญ์„ ์ด์šฉํ•˜๋Š๋ƒ์˜ ์ฐจ์ด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

  • ๋ฆฌํ„ฐ๋Ÿด์„ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด ๊ฐ์ฒด๋Š” String pool์— ์ €์žฅ ๋˜๊ณ , ์ด ๋•Œ String pool์„ ๊ฒ€์‚ฌํ•˜์—ฌ ๊ฐ’์ด ๋™๋“ฑํ•œ ๊ฒฝ์šฐ ๊ฐ™์€ ๊ณณ์„ ์ฐธ์กฐํ•˜๋„๋ก ์„ค์ •๋˜์–ด ์žˆ์–ด ๋™์ผ์„ฑ๊นŒ์ง€ ๋ณด์žฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ๊ฐ์˜ ๊ฐ์ฒด๋Š” Heap ์˜์—ญ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๊ณต๊ฐ„์— ์ €์žฅ๋˜๊ณ , ์ด ๊ฒฝ์šฐ์—๋Š” ๊ฐ’์ด ๊ฐ™๋”๋ผ๋„ ์ฐธ์กฐ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผ์„ฑ์„ ๋ณด์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋‘˜ ๋‹ค Heap ์˜์—ญ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์˜ ๋Œ€์ƒ์ด ๋œ๋‹ค๋Š” ์ ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

<์ฐธ๊ณ  ์ž๋ฃŒ>
 

 

[JAVA] ์ƒ์ˆ˜ ํ’€(String Pool)์€ GC๊ฐ€ ๋ ๊นŒ?

์ƒ์ˆ˜ ํ’€(String Pool)์€ GC๊ฐ€ ๋ ๊นŒ์š”? JVM ๋‚ด๋ถ€๊ตฌ์กฐ๋ฅผ ๋‹ค์‹œ ๋ณด๋˜ ์ค‘์—, ๋ฌธ๋“ "์ƒ์ˆ˜ ํ’€์€ GC๊ฐ€ ๋ ๊นŒ?" ๋ผ๋Š” ์ƒ๊ฐ์ด ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค. ์ž๋ฐ”์—์„œ ๋ณดํ†ต static์€ GC๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋ฐ, ์ƒ์ˆ˜ํ’€๋„ GC๊ฐ€ ์•ˆ๋˜๋ ค๋‚˜? ์ž๋ฐ”์—

junghyungil.tistory.com

 

 

String Constant Pool์ด๋ž€? | Java String Pool

Java์—์„œ String ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ฒซ๋ฒˆ์งธ๋Š” String literal, ์ฆ‰ ํฐ ๋”ฐ์˜ดํ‘œ("")๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๊ณ , ๋‘๋ฒˆ์งธ๋Š” new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฐฉ๋ฒ•์—๋Š” ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ? ๊ฐ„๋‹จํ•œ

starkying.tistory.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ