본문 바로가기

시즌1/Java Tips

[Java Tips] 자바 프로그램 메모리 사용량 보기


자바로 개발하다 보면, 헌재 전체 메모리 양과 사용 가능한 메모리 양을 확인해야 할 때가 있습니다.

메모리와 관련된 기능은 Runtime class에서 제공해 주며 다음과 같이 사용할 수 있습니다.

각 Method 들은 byte 단위의 메모리 양을 리턴합니다.

  • totalMemory : 현재 Allocate 된 메모리 사용량을 리턴
  • freeMemory : 현재 Allocate 된 메모리 중, 사용 가능한 메모리 양을 리턴

 

public class MemoryTrace {

	public static void main(String[] args) {

		System.out.println("Total Memory : " + Runtime.getRuntime().totalMemory());
		System.out.println("Free Memory : " + Runtime.getRuntime().freeMemory());
		System.out.println("Used Memory : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));

	}
}

 Output :
Total Memory : 5177344
Free Memory : 4955832
Used Memory : 221512