반응형
자바 API에서 제공하는 것은 가상머신 관련된 것 뿐이라서 실제 메모리하고는 상관이 없습니다.

JNI를 사용해도 되겠지만 C 프로그램과는 친하고 싶지 않아서리... ^^ 꽁수를 부려봤습니다.



윈도우 플랫폼의 메모리 사용량은 systeminfo 라는
윈도우 명령을 사용해서 가능하기는 합니다만...
윈도 XP 이상에서만 지원되죠. 98 같은데서는 안됩니다 =.=

import java.io.*;

public class MemoryInfo {
public MemoryInfo() {
}
public static void main(String[] args) {
try {
String output, err;
String[] cmd = {"systeminfo.exe"};
Process proc = Runtime.getRuntime().exec(cmd);

BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

try {
while ( (output = stdout.readLine()) != null) {
if (output.indexOf("메모리") > 0) {
System.out.println(output);
}
}
while ( (err = stderr.readLine()) != null) {
System.out.println(err);
}
}
catch (IOException e) {
System.exit(0);
}
}
catch (IOException e1) {
System.err.println(e1);
System.exit(1);
}
}
}

============== 실행 결과 =============

총 실제 메모리: 511MB
사용 가능한 실제 메모리: 158MB
가상 메모리: 최대 크기: 2,048MB
가상 메모리: 사용 가능: 2,004MB
가상 메모리: 사용 중: 44MB
<출처:ibm.com/developerworks/kr>

+ Recent posts