[Java] 메모리 확인

이민아·2023년 7월 14일
0

Java

목록 보기
1/4
post-thumbnail

🔥자바 메모리 확인 방법

	int TotalMemory = (int)(Runtime.getRuntime().totalMemory() / (1024*1024));
	int FreeMemory = (int)(Runtime.getRuntime().freeMemory() / (1024*1024));
	int usedMemory = (int)(TotalMemory - FreeMemory);
	float FreeRatio = (float)((Runtime.getRuntime().freeMemory()*1.0 / Runtime.getRuntime().totalMemory()) * 100.0);
	DecimalFormat formatter = new DecimalFormat("###,###M");
	DecimalFormat formatter2 = new DecimalFormat("#,###.#");
	String strStyle = null;
    
	if(FreeRatio > 80){
		strStyle = "background-color:white;color:black";
	}else if (FreeRatio <= 80 && FreeRatio> 60){
		strStyle = "background-color:white;color:blue";
	}else if (FreeRatio <= 60 && FreeRatio> 40){
		strStyle = "background-color:white;color:red";
	}else if (FreeRatio <= 40){
		strStyle = "background-color:red;color:white";
	}	
	String  drive, color;
	double  totalSize, usedSize, useableSize, ratio;        
	File[] roots = File.listRoots();
	
	
	OperatingSystemMXBean osbean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();		
	String cpuUsage = String.format("%.2f", osbean.getSystemCpuLoad() * 100);
	
	MemoryMXBean membean = (MemoryMXBean)ManagementFactory.getMemoryMXBean();
	MemoryUsage heap = membean.getHeapMemoryUsage();
	MemoryUsage nonheap = membean.getNonHeapMemoryUsage();
	long heapInit = heap.getInit();
	long heapUsed = heap.getUsed();
	long heapCommit = heap.getCommitted();
	long heapMax = heap.getMax();
	long nonheapMax = nonheap.getMax();	
	long nonheapUsed = nonheap.getUsed();
	double noeheapUsedPercent = ((nonheapUsed * 1.0) / nonheapMax) * 100.0;

CPU사용률  : <%=cpuUsage %>%<br/>
Heap : <%=formatter.format(TotalMemory)%> - <%=formatter.format(usedMemory)%>(<%=formatter2.format(100-FreeRatio)%>%) = <%=formatter.format(FreeMemory)%>(<%=formatter2.format(FreeRatio)%>%)<br/>

for (File root : roots) {
	drive = root.getAbsolutePath();
	totalSize = root.getTotalSpace() / Math.pow(1024, 3);
	useableSize = root.getUsableSpace() / Math.pow(1024, 3);
	usedSize = totalSize - useableSize;
	if( totalSize < 10 ){	// CD
		continue;
	}	
	if( totalSize > 0 ){
		ratio = Math.round((useableSize / totalSize) * 100.0);
		color = "black";
		if(ratio <= 50){
		  color = "blue";
		}
		if(ratio <= 10){
		  color = "red";
		}
        
        <font color='<%=color%>'><%=drive%> : <%=(Math.round(totalSize*100)/100.0)%> - <%=(Math.round(usedSize*100)/100.0)%>(<%=String.format("%.1f",1.0*Math.round(100.0-ratio))%>%) = <%= (Math.round(useableSize*100)/100.0)%> GB(<%=ratio%>%)</font><br/>
        
	}
}        

📕 Runtime Class

: Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
An application cannot create its own instance of this class

* getRuntime()

: Returns the runtime object associated with the current Java application.

* totalMemory() - type : long

: Returns the total amount of memory in the Java virtual machine(JVM).
: JVM의 모든 메모리양
: 1024 *1024 로 나누는 이유? MB로 단위를 변경하기 위함.

* freeMemory() - type : long

: Returns the amount of free memory in the Java Virtual Machine.
: JVM의 남은 메모리양

📗 OperatingSystemMXBean

: This interface defines several convenient methods for accessing system properties about the operating system on which the Java virtual machine is running.

getSystemCpuLoad

: Returns the "recent cpu usage" for the whole system.
: system의 cpu load값을 0.0~1.0으로 리턴한다.
: 0.0은 0%, 1.0이면 100%를 의미

📘 MemoryMXBean

: The management interface for the memory system of the Java virtual machine.

getHeapMemoryUsage()

: Returns the current memory usage of the heap that is used for object allocation.
: 객체의 할당에 사용되는 heap의 현재의 메모리 사용량을 리턴합니다.

getNonHeapMemoryUsage()

: Returns the current memory usage of non-heap memory that is used by the Java virtual machine.
: Java 가상 머신에 의해 사용되는 heap 이외의 현재의 메모리 사용량을 리턴합니다.

📙 File.listRoots()

: 드라이브 나열

getAbsolutePath()

: 파일의 절대 경로를 문자열로 넘겨줌.

getTotalSpace()

: 하드디스크의 총 용량을 리턴.

getUsableSpace()

: 하드디스크의 사용 가능한 용량을 리턴

참고 : https://injunech.tistory.com/274

profile
IT업계 종사자

0개의 댓글