๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋‚˜์˜ ๋ชจ์–‘

019 | Java - I/O, Thread, JVM ๋ณธ๋ฌธ

SEB/TIL

019 | Java - I/O, Thread, JVM

kexon 2022. 7. 19. 23:13

๐Ÿ’™ File I/O

๐Ÿค InputStream / OutputStream

๋”๋ณด๊ธฐ
  • File ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ⇒ ๋ฐ”์ดํŠธ ๊ธฐ๋ฐ˜
    • ๋ฐ”์ดํŠธ๊ธฐ๋ฐ˜ ⇒ ์ž…์ถœ๋ ฅ๋‹จ์œ„: 1byte
  • ์ŠคํŠธ๋ฆผ์€ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก ⇒ ์ž…์ถœ๋ ฅ ๋™์‹œ์— ์ฒ˜๋ฆฌ ⇒ ๊ฐ๊ฐ์˜ ์ŠคํŠธ๋ฆผ์ด ํ•„์š”
  • ์ž๋ฐ”์—์„œ ์ž…์ถœ๋ ฅ์„ ๋‹ค๋ฃจ๊ธฐ ⇒ InputStream / OutputStream
    • ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์€ ์–ด๋–ค ๋Œ€์ƒ์„ ๋‹ค๋ฃจ๋А๋ƒ์— ๋”ฐ๋ผ ์ข…๋ฅ˜๊ฐ€ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.
    • File์„ ๋‹ค๋ฃฐ ๋•Œ: FileInputStream / FileOutputStream ์‚ฌ์šฉ
    • ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹ค๋ฃฐ ๋•Œ: PipedInputStream / PipedOutputStream ์‚ฌ์šฉ

FileInputStream

echo FileInputStream >> fis.txt
  • ๋ณด์กฐ์ŠคํŠธ๋ฆผ์ธ BufferedInputStream ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ ํ–ฅ์ƒ
    • ๋ฒ„ํผ == ๋ฐ”์ดํŠธ๋ฐฐ์—ด
      • ์—ฌ๋Ÿฌ ๋ฐ”์ดํŠธ๋ฅผ ์ €์žฅํ•˜์—ฌ ํ•œ๋ฒˆ์— ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ž„์‹œ ์ €์žฅ ๊ณต๊ฐ„
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamEx {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("/practice/fis.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);

            int i = 0;
            while((i = fis.read()) != -1) {  // fis.read()์˜ ๋ฆฌํ„ด๊ฐ’์„ i์— ์ €์žฅํ•œ ํ›„ ๊ฐ’์ด -1์ธ์ง€ ํ™•์ธ
                System.out.println((char)i);
            }

            fis.close();
        } catch (Exception e) {
            System.out.println("์—๋Ÿฌ: " + e.getMessage());
        }
    }
}

/*์ถœ๋ ฅ ๊ฒฐ๊ณผ
practice ํด๋” ์•ˆ์— FileInputStream์ด๋ผ๋Š” ํ…์ŠคํŠธ๊ฐ€ ๋“ค์–ด์žˆ๋Š” fis.txt๋ผ๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋จ
*/

FileOutputStream

import java.io.FileOutputStream;

public class FileOutputStreamEx {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("/practice/fos.txt");
            String word = "FileOutStream";

            byte b[] = word.getBytes();
            fos.write(b);
            fos.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

/*์ถœ๋ ฅ ๊ฒฐ๊ณผ
practice ํด๋” ์•ˆ์— FileOutStream์ด๋ผ๋Š” ํ…์ŠคํŠธ๊ฐ€ ๋“ค์–ด์žˆ๋Š” fos.txt๋ผ๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋จ
*/

๐Ÿค FileReader / FileWriter

๋”๋ณด๊ธฐ
  • ๋ฌธ์ž ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ: ๋ฌธ์ž ๋ฐ์ดํ„ฐ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉ
    • ์œ ๋‹ˆ์ฝ”๋“œ(UTF-16)๊ฐ„์˜ ๋ณ€ํ™˜์„ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ
  • ๋ฌธ์ž ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ์—์„œ๋Š”
    • InputStream → Reader / OutputStream → Writer
    • FileInputStream → FileReader
    • FileOutputStream→ FileWriter
  • FileReader: ์ธ์ฝ”๋”ฉ → ์œ ๋‹ˆ์ฝ”๋“œ
  • FileWriter๋Š”: ์œ ๋‹ˆ์ฝ”๋“œ → ์ธ์ฝ”๋”ฉ

FileReader

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderEx {
    public static void main(String[] args) {

        try {
            String fileName = "/practice/fis.txt";
				 // FileReader file = new FileReader(fileName);
            FileInputStream file = new FileInputStream(fileName);

            int data = 0;

            while((data = file.read()) != -1) {
                System.out.println((char)data);
            }
            file.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
  • ํ•œ๊ธ€ ์ž…๋ ฅ ํ›„ FileReader → FileInputStream ๋ณ€๊ฒฝ ⇒ ํ•œ๊ธ€ ๊นจ์ง
  • ์„ฑ๋Šฅ ๊ฐœ์„ : BufferedReader

FileWriter

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterEx {
    public static void main(String[] args) {
        try {
            String fileName = "/Users/keson/practice/practice.txt";
            FileWriter writer = new FileWriter(fileName);

            String str = "written";
            writer.write(str);
            writer.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

/* ์ถœ๋ ฅ ๊ฒฐ๊ณผ
practice.txt ํŒŒ์ผ์— written ๋‹จ์–ด ์จ์ง
*/

๐Ÿค File

๋”๋ณด๊ธฐ
  • File ํด๋ž˜์Šค๋กœ ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ† ๋ฆฌ์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
import java.io.File;
import java.io.IOException;

public class FileEx {
    public static void main(String[] args) throws IOException {
        File file = new File("../practice.txt");

        System.out.println(file.getPath());
        System.out.println(file.getParent());
        System.out.println(file.getCanonicalPath());
        System.out.println(file.canWrite());
    }
}
  • ํŒŒ์ผ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ != ํŒŒ์ผ ์ƒ์„ฑ
    ํŒŒ์ผ์„ ์ƒ์„ฑ: ํŒŒ์ผ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ ์‹œ→ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— ๊ฒฝ๋กœ → ๋‘ ๋ฒˆ์งธ ์ธ์ž์— ํŒŒ์ผ๋ช… → createNewFile() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

์˜ˆ์‹œ

ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ(.)์—์„œ ํ™•์žฅ์ž๊ฐ€ .txt์ธ ํŒŒ์ผ๋ช… ์•ž์— “code”๋ผ๋Š” ๋ฌธ์ž์—ด ๋ถ™์—ฌ์ฃผ๊ธฐ

import java.io.File;

public class FileNameEx {
    public static void main(String[] args) {
        File parentDir = new File("./");
        File[] list = parentDir.listFiles();

        String prefix = "code";

        for(int i = 0; i < list.length; i++) {
            String fileName = list[i].getName();

            if(fileName.endsWith("txt") && !fileName.startsWith("code")) {
                list[i].renameTo(new File(parentDir, prefix + fileName));
            }
        }
    }
}

โœ… Ref. 

๋”๋ณด๊ธฐ
  • FileInputStream
 

InputStream (Java Platform SE 7 )

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown. If

docs.oracle.com

  • FileOutputStream
 

OutputStream (Java Platform SE 7 )

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such b

docs.oracle.com

  • reader
 

Reader (Java Platform SE 7 )

reset public void reset() throws IOException Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for ex

docs.oracle.com

  • writer
 

Writer (Java SE 11 & JDK 11 )

Returns a new Writer which discards all characters. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect. While the stream is open, the append(char), append(CharSequence), app

docs.oracle.com

  • File ๋ฉ”์„œ๋“œ ํ™•์ธ
 

File (Java Platform SE 7 )

Returns the absolute pathname string of this abstract pathname. If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the path

docs.oracle.com

๐Ÿ’™ Thread

๐Ÿค ์“ฐ๋ ˆ๋“œ

๋”๋ณด๊ธฐ

ํ”„๋กœ์„ธ์Šค์™€ ์“ฐ๋ ˆ๋“œ

  • ํ”„๋กœ๊ทธ๋žจ: ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ
  • ํ”„๋กœ์„ธ์Šค: ์‹คํ–‰์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ
    • ๊ตฌ์„ฑ์š”์†Œ: ๋ฐ์ดํ„ฐ, ์ปดํ“จํ„ฐ ์ž์›, ์Šค๋ ˆ๋“œ
  • ์“ฐ๋ ˆ๋“œ: ํ”„๋กœ์„ธ์Šค์—์„œ ์‹คํ–‰๋˜๋Š” ์†Œ์Šค์ฝ”๋“œ์˜ ์‹คํ–‰ํ๋ฆ„
    • ์‹ฑ๊ธ€ ์“ฐ๋ ˆ๋“œ ํ”„๋กœ์„ธ์Šค: ํ•˜๋‚˜์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ํ”„๋กœ์„ธ์Šค
    • ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ”„๋กœ์„ธ์Šค: ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ํ”„๋กœ์„ธ์Šค

๋ฉ”์ธ์“ฐ๋ ˆ๋“œ

  • ์ž๋ฐ”๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ ⇒ main ๋ฉ”์„œ๋“œ ์‹คํ–‰
    • ์ฝ”๋“œ์˜ ๋์ด๋‚˜ return๋ฌธ์—์„œ ์‹คํ–‰ ์ข…๋ฃŒ

๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค
  • ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋”ฉ: ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ

๐Ÿค ์“ฐ๋ ˆ๋“œ์˜ ์ƒ์„ฑ๊ณผ ์‹คํ–‰

๋”๋ณด๊ธฐ

์ž‘์—… ์“ฐ๋ ˆ๋“œ์˜ ์ƒ์„ฑ๊ณผ ์‹คํ–‰

  • ๋ณ„๋„์˜ ์ž‘์—… ์“ฐ๋ ˆ๋“œ๋ฅผ ํ™œ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ?
    ⇒ ์ž‘์—… ์“ฐ๋ ˆ๋“œ๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์ž‘์—… ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ฒƒ

run() ๋ฉ”์„œ๋“œ๋กœ ์ž‘์—… ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • Runnable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ์ฒด์—์„œ run()์„ ๊ตฌํ˜„ํ•˜์—ฌ ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹คํ–‰
public class ThreadEx1 {
    public static void main(String[] args) {
        // 4. ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ
        // 4-1. Runnable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ์ฒด ํ™œ์šฉ
        Runnable task1 = new ThreadTask1();

        // 4-2.Runnable ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉด์„œ Thread ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•ด์„œ ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ
        Thread thread1 = new Thread(task1);

        // 4-1, 4-2 ํ•œ์ค„๋กœ
        // Thread thread1 = new Thread(new ThreadTask1());

        // 5. ์ž‘์—… ์“ฐ๋ ˆ๋“œ ์‹คํ–‰ -> run() ๋‚ด๋ถ€ ์ฝ”๋“œ ์ฒ˜๋ฆฌ
        thread1.start();

        // 6. ๋ฐ˜๋ณต๋ฌธ ์ถ”๊ฐ€
        for(int i = 0; i < 50; i++) {
            System.out.println("@");
        }
    }
}

// 1. Runnable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค ์ž‘์„ฑ
class ThreadTask1 implements Runnable {

    // 2. run() ๋ฉ”์„œ๋“œ ์ž‘์„ฑ
    public void run() {
        // 3. ์“ฐ๋ ˆ๋“œ๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ž‘์—… ๋‚ด์šฉ ์ž‘์„ฑ
        for(int i = 0; i < 50; i++) {
            System.out.println("#");
        }
    }
}
  • Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์€ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ run()์„ ๊ตฌํ˜„ํ•˜์—ฌ ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹คํ–‰
public class ThreadEx2 {
    public static void main(String[] args) {
        // 4. Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜์—ฌ ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ
        Thread thread2 = new ThreadTask2();

        // 5. ์ž‘์—… ์“ฐ๋ ˆ๋“œ ์‹คํ–‰ -> run() ๋‚ด๋ถ€ ์ฝ”๋“œ ์ฒ˜๋ฆฌ
        thread2.start();

        // 6. ๋ฐ˜๋ณต๋ฌธ ์ถ”๊ฐ€
        for(int i = 0; i < 100; i++) {
            System.out.println("@");
        }
    }
}

// 1. Thread ํด๋ž˜์Šค ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค ์ž‘์„ฑ
class ThreadTask2 extends Thread {

    // 2. run() ๋ฉ”์„œ๋“œ ์ž‘์„ฑ
    public void run() {
        // 3. ์“ฐ๋ ˆ๋“œ๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ž‘์—… ๋‚ด์šฉ ์ž‘์„ฑ
        for(int i = 0; i < 100; i++) {
            System.out.println("#");
        }
    }
}

⇒ ๋‘ ๋ฐฉ๋ฒ• ๋‹ค ์ž‘์—… ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ๋™์ผํ•œ ๋‚ด๋ถ€ ๋™์ž‘ ์ˆ˜ํ–‰(run() ๋ฉ”์„œ๋“œ์— ์ž‘์„ฑ๋œ ์ฝ”๋“œ ์ฒ˜๋ฆฌ)

์ต๋ช… ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ ์“ฐ๋ ˆ๋“œ์˜ ์ƒ์„ฑ๊ณผ ์‹คํ–‰

  • Runnable ์ต๋ช… ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•œ ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ ๋ฐ ์‹คํ–‰
public class ThreadEx3 {
    public static void main(String[] args) {
        // ์ต๋ช… Runnable ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•œ ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ
        Thread thread3 = new Thread(new Runnable() {
            public void run() {
                for(int i = 0; i < 100; i++) {
                    System.out.print("#");
                }
            }
        });

        thread3.start();

        for(int i = 0; i < 100; i++) {
            System.out.print("@");
        }
    }
}
  • Thread ์ต๋ช… ํ•˜์œ„ ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•œ ์Šค๋ ˆ๋“œ ์ƒ์„ฑ ๋ฐ ์‹คํ–‰
public class ThreadEx4 {
    public static void main(String[] args) {
        // ์ต๋ช… Thread ํ•˜์œ„ ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•œ ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ
        Thread thread4 = new Thread() {
            public void run() {
                for(int i = 0; i < 100; i++) {
                    System.out.print("#");
                }
            }
        };

        thread4.start();

        for(int i = 0; i < 100; i++) {
            System.out.print("@");
        }
    }
}

๐Ÿค ์“ฐ๋ ˆ๋“œ ๋™๊ธฐํ™”

๋”๋ณด๊ธฐ
  • ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์“ฐ๋ ˆ๋“œ๋งŒ ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ฒด์— ๋ฝ(lock)์„ ๊ฑธ์–ด ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ

์ž„๊ณ„ ์˜์—ญ(Critical section) / ๋ฝ(Lock)

  • ์ž„๊ณ„ ์˜์—ญ: ๋‹จ ํ•˜๋‚˜์˜ ์“ฐ๋ ˆ๋“œ๋งŒ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ์˜์—ญ
  • ๋ฝ: ์ž„๊ณ„ ์˜์—ญ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ

๋ฉ”์„œ๋“œ ์ „์ฒด๋ฅผ ์ž„๊ณ„์˜์—ญ์œผ๋กœ ์ง€์ •

  • ๋ฉ”์„œ๋“œ ๋ฐ˜ํ™˜ํƒ€์ž… ์™ผ์ชฝ์— synchronized ๋ถ™์—ฌ์ฃผ๋ฉด, ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ์„ ๋•Œ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•  ์Šค๋ ˆ๋“œ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํฌํ•จ๋œ ๊ฐ์ฒด์˜ ๋ฝ์„ ์–ป์Œ

ํŠน์ • ์˜์—ญ์„ ์ž„๊ณ„์˜์—ญ์œผ๋กœ ์ง€์ •

  1. synchronized ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ ์†Œ๊ด„ํ˜ธ(()) ์•ˆ์— ํ•ด๋‹น ์˜์—ญ์ด ํฌํ•จ๋œ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๋„ฃ์Œ
  2. ์ค‘๊ด„ํ˜ธ({})๋กœ ๋ธ”๋Ÿญ์„ ์—ด์–ด ๋ธ”๋Ÿญ ๋‚ด์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ

โœ… Ref. 

๐Ÿ’™ JVM(Java Virtual Machine)

๋”๋ณด๊ธฐ

JVM

  • ์ž๋ฐ”๋กœ ์ž‘์„ฑํ•œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•ด ์‹คํ–‰ํ•˜๋Š” ๋ณ„๋„์˜ ํ”„๋กœ๊ทธ๋žจ

JVM ๊ตฌ์กฐ

JVM ๋ฉ”๋ชจ๋ฆฌ๊ตฌ์กฐ

Stack ์˜์—ญ

  • Stack: ์ผ์ข…์˜ ์ž๋ฃŒ๊ตฌ์กฐ(ํ”„๋กœ๊ทธ๋žจ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹)
  • LIFO(Last In First Out): ๋งˆ์ง€๋ง‰์— ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ด

Heap ์˜์—ญ

  • JVM์ด ์ž‘๋™๋˜๋ฉด ์ž๋™ ์ƒ์„ฑ๋˜๋Š” ์˜์—ญ์œผ๋กœ, ๊ฐ์ฒด๋‚˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, ๋ฐฐ์—ด์ด ์ €์žฅ
Piano piano = new Piano();
  • new Piano() ์‹คํ–‰→ Heap ์˜์—ญ์— ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ → ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ ์œ„์น˜์˜ ์ฃผ์†Œ๊ฐ’์„ piano์— ํ• ๋‹น
    ⇒ piano๋Š” Stack ์˜์—ญ์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜
  • ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃฌ๋‹ค๋Š” ๊ฒƒ?์ •๋ฆฌํ•˜์ž๋ฉด, Heap ์˜์—ญ์€ ์‹ค์ œ ๊ฐ์ฒด์˜ ๊ฐ’์ด ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„
    ⇒ Stack ์˜์—ญ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด Heap ์˜์—ญ์— ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ

Garbage Collection

  • ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฐ์ฒด๋ฅผ ์ฐพ์•„ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ™•๋ณดํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ž๋™ ๊ด€๋ฆฌ ํ”„๋กœ์„ธ์Šค

โœ… Ref. 

๋”๋ณด๊ธฐ

๐Ÿ’œ ์˜ค๋Š˜์˜ ์ƒ๊ฐ ์กฐ๊ฐ๋ชจ์Œ

  • ๐Ÿ˜ญ

'SEB > TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

024 | Data Structure - Tree, Graph  (0) 2022.07.26
023 | Data Structure - Stack & Queue  (0) 2022.07.25
018 | Java - Enum, Annotation, Lambda, Stream  (0) 2022.07.18
017 | Java - ๐Ÿฅ Practical | Collection Framework  (0) 2022.07.16
016 | Java - Generics, Collection Framework  (2) 2022.07.15
Comments