[SPRING #13] 메모리 구조

JVM의 구성을 살펴보면 크게 4가지(Class Loader, Execution Engine, Garbage Collector, Runtime Data Area)로 나뉜다.

1. Class Loader

자바에서 소스를 작성하면 Person.java 처럼 .java파일이 생성된다.

.java 소스를 자바컴파일러가 컴파일하면 Person.class 같은 .class파일(바이트코드)이 생성된다.

이렇게 생성된 클래스파일들을 엮어서 JVM이 운영체제로부터 할당받은 메모리영역인 Runtime Data Area로 적재하는 역할을 Class Loader가 한다. (자바 애플리케이션이 실행중일 때 이런 작업이 수행된다.)

2. Execution Engine

Class Loader에 의해 메모리에 적재된 클래스(바이트 코드)들을 기계어로 변경해 명령어 단위로 실행하는 역할을 한다.

명령어를 하나 하나 실행하는 인터프리터(Interpreter)방식이 있고 JIT(Just-In-Time) 컴파일러를 이용하는 방식이 있다.

3. Garbage Collector

Garbage Collector(GC)는 Heap 메모리 영역에 생성(적재)된 객체들 중에 참조되지 않는 객체들을 탐색 후 제거하는 역할을 한다.

4. Runtime Data Area

JVM의 메모리 영역으로 자바 애플리케이션을 실행할 때 사용되는 데이터들을 적재하는 영역이다.

이 영역은 크게 Method Area, Heap Area, Stack Area, PC Register, Native Method Stack로 나눌 수 있다.

1) Method Area ( = class area = static area)
클래스 정보를 처음 메모리 공간에 올릴 때 초기화 되는 대상을 저장하기 위한 메모리 공간

ex) static 변수

  • 여러 개의 인스턴스가 같은 메모리의 값을 공유하기 위해 사용
  • static 변수는 인스턴스가 생성될 때 마다 다른 메모리를 가지는 것이 아니라 프로그램이 메모리에 적재(load)될 때 데이터 영역의 메모리에 생성 됨.

2) Stack Area
프로그램 실행과정에서 임시로 할당되었다가 메소드를 빠져나가면 바로 소멸되는 특성의 데이터를 저장하기 위한 영역이다. 각종 형태의 변수나 임시 데이터, 스레드나 메소드 정보를 저장한다.

public class FunctionTest{
	public static void main(String[] args){
    	int num1 = 10;
        int num2 = 30;
        
        int sum = addNum(num1,num2);
        System.out.println(sum);
    }
    
    public static int addNum(int n1, int n2){
    	int result = n1 + n2;
        return result;
    }
}

함수 호출과 스택 메모리 구조

3) Heap Area
new 키워드로 생성된 객체와 배열이 생성되는 영역이다.

메소드 영역에 로드된 클래스만 생성이 가능하고 Garbage Collector가 참조되지 않는 메모리를 확인하고 제거하는 영역이다.

public class Student{
    //멤버변수
    int studentId;
    String studentName;
    String address;
    
   //메서드
   public void getStudetName(){
   	return studentName;
   }
   
   public static void main(String[] args){
   	Student studentLee = new Student();
        studentLee.studentName = "Lee";
        studentLee.studentId = 100;
        studentLee.address = "서울시";
   }
}

student 객체를 생성하면 힙 메모리에 studentName,Id,address등 생성되고 stack에 studentLee 지역변수(참조변수)는 그 힙 메모리 주소값이 저장된다.

정리!

package staticex;

class Student{
	
    static int serialNum = 10000;
    
    int studentId;
    
    public Student(){
    	serialNum++;
        studentId = serialNum;
    }
}


public class StudentTest1 {
	
    System.out.println(Student.serialNum);
    
    Student studentLee = new Student();
    System.out.println(studentLee.studentId);
    
    Student studentSon = new Student();
    System.out.println(studentSon.studentId);
   
    
}

10000
10001
10002

부동소수점
https://codetorial.net/articles/floating_point.html

https://asfirstalways.tistory.com/158
출처: https://jeong-pro.tistory.com/148 [기본기를 쌓는 정아마추어 코딩블로그]

좋은 웹페이지 즐겨찾기