简体   繁体   English

如何使用SIGAR库获取Java进程的内存使用情况?

[英]how to get memory usage of a java process using SIGAR library?

The process below is executing a c++ code.I want to know the memory used by the c++ code. 下面的过程正在执行一个c ++代码。我想知道c ++代码使用的内存。

ProcessBuilder p = new ProcessBuilder("./" + compiledFile);
Process pp = p.start();
long id = getProcessID(pp);
Sigar sigar=new Sigar();
ProcMem memory=new ProcMem();
memory.gather(sigar, id);
System.out.println("memory ---> "+Long.toString(memory.getSize()));

I have tried the follwing code.But it returns sigar exception: "No such process". 我已经尝试了以下代码。但是它返回sigar异常:“无此过程”。 To get the process id I am using following method 要获取进程ID,我正在使用以下方法

public long getProcessID(Process p)
    {
        long pid = -1;
        try
        {
            if (p.getClass().getName().equals("java.lang.UNIXProcess")) 
            {
                Field f = p.getClass().getDeclaredField("pid");
                f.setAccessible(true);
                result = f.getLong(p);
                f.setAccessible(false);
            }
        }
        catch(Exception ex)
        {
            pid = -1;
        }
        return pid;
    }

I am a novice.Did I get something wrong? 我是新手,我做错什么了吗? Or Am i doing something wrong? 还是我做错了什么? any help or suggestion will be much appreciated..thanks. 任何帮助或建议将不胜感激..谢谢。

Have you looked at this class . 你看过这堂课吗? It is not elegant at all, but seems to be an example of what you would want to do. 它一点也不优雅 ,但是似乎是您想要做的一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM