Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Java程序中检索可用RAM的数量?_Java_Memory_Operating System - Fatal编程技术网

如何从Java程序中检索可用RAM的数量?

如何从Java程序中检索可用RAM的数量?,java,memory,operating-system,Java,Memory,Operating System,可用RAM:我的目标指标。 Java:我选择的工具。 ?:使用后者获取前者的一个好方法。这有一些有用的信息。可能是这样,使用Java本机接口(JNI): 如果不借助非便携或本机库,就很难做到这一点 差不多 Runtime.getRuntime().freeMemory() 将只返回JVM可用的内存,这可能与系统范围的可用内存不同 这个页面提供了一个很好的概述 我知道有两个项目已经购买,并对结果感到满意。两者都基于Windows的使用。当我将它嵌入到我们当前的项目中时,我们想知道当用户启动我们的

可用RAM:我的目标指标。
Java:我选择的工具。

:使用后者获取前者的一个好方法。

这有一些有用的信息。

可能是这样,使用Java本机接口(JNI):


如果不借助非便携或本机库,就很难做到这一点

差不多

Runtime.getRuntime().freeMemory()
将只返回JVM可用的内存,这可能与系统范围的可用内存不同

这个页面提供了一个很好的概述


我知道有两个项目已经购买,并对结果感到满意。两者都基于Windows的使用。当我将它嵌入到我们当前的项目中时,我们想知道当用户启动我们的应用程序(WebStart)时有多少可用的ram,因为有很多性能投诉难以调查(我们怀疑ram问题)。JNIWrapper帮助我们在启动时收集关于空闲ram、总计和CPU等的统计数据,因此如果用户组抱怨,我们可以检查我们的统计数据,看看他们是否得到了不可靠的机器。救生。

通过执行命令
Free-m
获取空闲RAM,然后将其解释为:

Runtime runtime = Runtime.getRuntime();

BufferedReader br = new BufferedReader(
    new InputStreamReader(runtime.exec("free -m").getInputStream()));

String line;
String memLine = "";
int index = 0;
while ((line = br.readLine()) != null) {
  if (index == 1) {
    memLine = line;
  }
  index++;
}
//                  total        used        free      shared  buff/cache   available
//    Mem:          15933        3153        9683         310        3097       12148
//    Swap:          3814           0        3814

List<String> memInfoList = Arrays.asList(memLine.split("\\s+"));
int totalSystemMemory = Integer.parseInt(memInfoList.get(1));
int totalSystemUsedMemory = Integer.parseInt(memInfoList.get(2));
int totalSystemFreeMemory = Integer.parseInt(memInfoList.get(3));

System.out.println("Total system memory in mb: " + totalSystemMemory);
System.out.println("Total system used memory in mb: " + totalSystemUsedMemory);
System.out.println("Total system free memory in mb: "   + totalSystemFreeMemory);
Runtime=Runtime.getRuntime();
BufferedReader br=新的BufferedReader(
新的InputStreamReader(runtime.exec(“free-m”).getInputStream());
弦线;
字符串memLine=“”;
int指数=0;
而((line=br.readLine())!=null){
如果(索引==1){
memLine=行;
}
索引++;
}
//可用的免费共享缓冲区/缓存总使用量
//成员:15933 3153 9683 310 3097 12148
//掉期:381403814
List memInfoList=Arrays.asList(memLine.split(\\s+));
inttotalsystemmemory=Integer.parseInt(memInfoList.get(1));
int totalSystemUsedMemory=Integer.parseInt(memInfoList.get(2));
int totalSystemFreeMemory=Integer.parseInt(memInfoList.get(3));
System.out.println(“以mb为单位的总系统内存:“+totalSystemMemory”);
System.out.println(“系统使用的总内存(mb):+totalSystemUsedMemory);
System.out.println(“以mb为单位的总系统可用内存:“+totalSystemFreeMemory”);

为什么标题开头是“网络”?在“程序可用内存”或“系统中未分配内存”中是“可用内存”?这是什么平台
Kernel32
当然不是J2SE类。@Stephen C,你说得对,我更新了帖子,是为了JNISince。既然问题是关于检索可用内存的大小,而不是“如何更容易地使用JNI”,你可能想发布一个关于如何实现前者的示例…@shog9。问题是如何获得Java中可用的免费RAM。我已经提出了实现这一点的最简单的方法。您能否提供一个JNIWrapper将提供的示例,与Adrian所写的进行对比(这似乎有点轻上下文)?我知道这个问题很模糊(“免费内存”可能意味着很多事情…),但如果你有什么特别的想法,你不妨分享一下。。。
Runtime runtime = Runtime.getRuntime();

BufferedReader br = new BufferedReader(
    new InputStreamReader(runtime.exec("free -m").getInputStream()));

String line;
String memLine = "";
int index = 0;
while ((line = br.readLine()) != null) {
  if (index == 1) {
    memLine = line;
  }
  index++;
}
//                  total        used        free      shared  buff/cache   available
//    Mem:          15933        3153        9683         310        3097       12148
//    Swap:          3814           0        3814

List<String> memInfoList = Arrays.asList(memLine.split("\\s+"));
int totalSystemMemory = Integer.parseInt(memInfoList.get(1));
int totalSystemUsedMemory = Integer.parseInt(memInfoList.get(2));
int totalSystemFreeMemory = Integer.parseInt(memInfoList.get(3));

System.out.println("Total system memory in mb: " + totalSystemMemory);
System.out.println("Total system used memory in mb: " + totalSystemUsedMemory);
System.out.println("Total system free memory in mb: "   + totalSystemFreeMemory);