用Java监视nonPagedPool(Memory)

时间:2014-07-31 21:03:55

标签: java memory

有没有办法用Java监视内存的nonPagedPool-Area? 我发现的唯一library只能读取正在使用的内存总量,但不能读取特定的内存区域。

1 个答案:

答案 0 :(得分:0)

我决定使用JNA并致电PSAPI.GetPerformanceInfo来解决问题。调用命令行可能是昂贵而且非常难看,因为我需要每秒检查正在运行的进程和nonPagedPool。
幸运的是,用JNA实现这一点并不困难:

主要班级:

import com.sun.jna.Native;

public class JNA_PSAPI_Test_optimized {

    public static native boolean GetPerformanceInfo(PerformanceInformationStruct pPerformanceInformation, int cb);

    static {
        Native.register("psapi");
    }

    public static void main(String[] args) throws InterruptedException {
        PerformanceInformationStruct tPerfInfo = new PerformanceInformationStruct();

        while (true){
            GetPerformanceInfo(tPerfInfo, 104);
            System.out.println("nonPagedPool (MB): "+tPerfInfo.KernelNonpaged * tPerfInfo.PageSize / Math.pow(1024,2));
            Thread.sleep(1000);
        }
    }
}

<强> STRUCT级:

import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;

public class PerformanceInformationStruct extends Structure {
    public int cb;
    public long CommitTotal;
    public long CommitLimit;
    public long CommitPeak;
    public long PhysicalTotal;
    public long PhysicalAvailable;
    public long SystemCache;
    public long KernelTotal;
    public long KernelPaged;
    public long KernelNonpaged;
    public long PageSize;
    public int HandleCount;
    public int ProcessCount;
    public int ThreadCount;
    public PerformanceInformationStruct() {
        super();
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[]{"cb", "CommitTotal", "CommitLimit", "CommitPeak", "PhysicalTotal", "PhysicalAvailable", "SystemCache", "KernelTotal", "KernelPaged", "KernelNonpaged", "PageSize", "HandleCount", "ProcessCount", "ThreadCount"});
    }
}