我已经下载了Sigar API(http://support.hyperic.com/display/SIGAR/Home),并希望在项目中使用它来获取有关正在运行的不同进程的信息。

我的问题是,我真的找不到真正有用的代码段来学习,而从他们网站上获得的javadoc并没有太大帮助,因为我不知道我应该寻找什么。

您有什么想法可以找到更多信息吗?

最佳答案

要查找pid(查找有关特定进程的信息所必需),可以使用ProcessFinder
查找单个进程pid的方法是findSingleProcess(String expression)。例子:

    Sigar sigar=new Sigar();
    ProcessFinder find=new ProcessFinder(sigar);
    long pid=find.findSingleProcess("Exe.Name.ct=explorer");
    ProcMem memory=new ProcMem();
    memory.gather(sigar, pid);
    System.out.println(Long.toString(memory.getSize()));

表达式语法是这样的:
Class.Attribute.operator=value

在哪里:
Class is the name of the Sigar class minus the Proc prefix.
Attribute is an attribute of the given Class, index into an array or key in a Map class.
operator is one of the following for String values:
eq - Equal to value
ne - Not Equal to value
ew - Ends with value
sw - Starts with value
ct - Contains value (substring)
re - Regular expression value matches
operator is one of the following for numeric values:
eq - Equal to value
ne - Not Equal to value
gt - Greater than value
ge - Greater than or equal value
lt - Less than value
le - Less than or equal value

更多信息在这里:http://support.hyperic.com/display/SIGAR/PTQL

10-08 02:06