- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Runtime.getRuntime().totalMemory()
和 freeMemory()
来计算内存。但是,我对结果感到困惑。
我已经阅读了以下帖子:
What are Runtime.getRuntime().totalMemory() and freeMemory()?
a question on Runtime.getRuntime().totalMemory()
这是我的演示代码:
package test;
class Memory
{
public static long used()
{
long total=Runtime.getRuntime().totalMemory();
long free=Runtime.getRuntime().freeMemory();
return (total-free);
}
}
package test;
import java.util.ArrayList;
public class MemTestQuestion {
private static final long _10M = 10000000;
public static void main(String[] args) {
int runCount=10;
for (int i = 0; i < runCount; i++) {
arrayListMemTest();
}
}
public static void arrayListMemTest()
{
long startTime = System.currentTimeMillis();
long startMem=Memory.used();
ArrayList<Integer>al= new ArrayList<Integer>();
for (int i = 0; i < _10M; i++) {
al.add(1000);
}
long endMem= Memory.used();
long endEndTime = System.currentTimeMillis();
long timeLast = endEndTime - startTime;
long memUsed = endMem-startMem;
System.out.print("lasts:"
+ timeLast + "ms = "+timeLast/1000.0+"s\t");
System.out.println("mem used:"
+memUsed+"bytes = "+new java.text.DecimalFormat("#.00").format(memUsed/(1024*1024.0))+"M");
System.gc();
}
}
这是当 runCount=1(位于 Main 方法中的变量)时的结果:
lasts:3606ms = 3.606s mem used:214644488bytes = 204.70M
这是 runCount=10 时的结果:
lasts:3643ms = 3.643s mem used:214644488bytes = 204.70M
lasts:389ms = 0.389s mem used:254054928bytes = 242.29M
lasts:366ms = 0.366s mem used:219163424bytes = 209.01M
lasts:242ms = 0.242s mem used:256265992bytes = 244.39M
lasts:222ms = 0.222s mem used:255523768bytes = 243.69M
lasts:225ms = 0.225s mem used:253843192bytes = 242.08M
lasts:253ms = 0.253s mem used:253967736bytes = 242.20M
lasts:236ms = 0.236s mem used:253994680bytes = 242.23M
lasts:234ms = 0.234s mem used:254066232bytes = 242.30M
lasts:233ms = 0.233s mem used:254091448bytes = 242.32M
最让我困惑的是,当runCount=10
时,最多的结果是240M左右。但是当runCount=1
时,结果是200M左右。
我猜这是因为JAVA JVM没有及时收集垃圾,所以第一个结果比较有说服力。 我说得对吗?如果没有,有人可以提供一些线索或其他建议吗?提前致谢。
演示代码的目的是尝试比较标准的 Java 容器和来自第三方的容器。
最佳答案
I guess this is because the JAVA JVM does not collect garbage in time, so the first result are more convicing. Am I right?
我不这么认为。运行 System.gc()
应该是一个“尽最大努力”收集所有垃圾的提示。现在。可以忽略该提示,但如果 GC 确实运行,您会期望它执行 >>full<< 收集,并且仅在完成时返回。应该没有 GC“跟上”的问题。
If not, can someone provide some clues or other suggestions?
这可能是由于 JIT 编译和其他 JVM 预热效果所致。
当您的应用程序启动时,您的代码方法和所有(传递地)使用的库方法都是字节码,并将由字节码解释器解释。在解释字节码时,JVM 会收集有关各种事物的统计信息(例如,协助分支预测)。最终,它决定对这些方法进行 JIT 编译。
那么这与您的观察有何关系?
统计信息存储在堆中,并且可以访问...直到 JIT 编译器“使用”它们。
JIT 编译器异步运行,并且在运行时还会使用堆空间来保存其数据结构。
在启动时,JVM 必须加载类,这也会占用堆空间来存放临时对象。
堆大小不是恒定的。它会增长(有时会缩小),具体取决于每个 GC 周期后有多少可用空间。
在 JVM 预热阶段,所有这些结合起来给出堆大小和堆空间使用的局部“高”和“低”。
关于java - 使用 Runtime.getRuntime().totalMemory() 和 freeMemory() 计算内存的结果令人困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34347655/
尝试使用一些自定义数据(-p 标志)调用 ping。由于我需要传递参数,因此使用接受字符串数组的 exec() 重载似乎很直观。它一直失败,所以我尝试了与一长串相同的精确输入,现在一切都很好。我不知道
在下面的例子中,Java中调用“php hello.php”的两种方法有什么区别? p1 对 p2? String[] commands; String command = "php hello.ph
我在编译程序时遇到的问题是java无法识别“.getRuntime()”方法。下面是我的代码: import java.lang.*; public class Temp {
我正在阅读有关如何在运行 android 2.2 的自定义设备(中文框)上不在 dalvik(例如,后台 native 进程)内运行的 android 中获取正在运行进程的 PID 的信息,似乎没有在
我在 Java 1.8 上调用 p = Runtime.getRuntime().exec(command)。该命令调用 bash 脚本如下: #!/bin/bash args=("$@") cat
Runtime.getRuntime().freeMemory() 返回空闲堆内存空间还是堆+非堆内存空间? 最佳答案 javadoc说: @return an approximation to th
我想在 java 程序中使用 Runtime.getRuntime().exec 完整的代码片段如下,但我的问题归结为当我在 Runtime.getRuntime().exec(command),但是
我正在我的程序中构建某种更新程序功能。当检测到更新时,程序应下载 updater.jar 并终止。之后,updater.jar 将下载该程序的新版本并删除旧版本。这就是我的主程序运行更新程序的方式:
当我在 Windows 命令提示符中运行此命令时,它会打开默认浏览器并加载相应的页面: START: http://google.com 我尝试使用下面的 Java 代码执行相同的操作,但收到错误消息
我试图在 java 中执行终端命令,但得到错误的结果(即使该命令正在执行)。当我将相同的命令粘贴到终端时,它会产生正确的输出。我猜测问题是由于重定向字符('')而发生的,但我还没有找到解决方案。我正在
我正在尝试让我的 java 程序从命令提示符运行 svn 命令,这会将日志写入 xml 文件。 这就是我想要它做的事情: Runtime.getRuntime().exec("cmd.exe /c s
我正在尝试通过Java将curl命令作为进程执行。当我执行它时,我根本没有得到任何输出。当我通过命令行执行相同的命令时,我得到了正确的输出。 URL 以 XML 形式返回输出。 这是我的 Java 程
我正在尝试将我编写的 2 个简单程序合并到一个 .jar 中。我将两个 .jars 打包成一个新的,并使用 Runtime.getRuntime().exec 方法来执行它们。 代码: public
我正在尝试从我的 java 应用程序运行 *.bat 文件(它能够运行多个命令并一一检索输出)。 我的目的是发送一个命令,读取输出,使用此输出作为第二个命令,然后再次检索输出。 为了实现这一目标,通过
我正在尝试计算此 stub 的运行时内存使用情况。这在 Eclipse 中工作正常,但是当我在 Linux 服务器上运行相同的代码时,它除了 0 之外不打印任何内容。服务器配置:64位,4核服务器,1
我发现以下代码使用 Runtime.getRuntime().exec 运行任意程序(例如 Notepad.exe )。 public class RuntimeDemo { public st
我创建了一个类来处理 Android 应用程序中的根命令,该类在大多数情况下都运行良好。我使用 Runtime.getRuntime().exec("su") 创建进程,然后使用 DataOutput
基本上,当我输入这些命令时手动打开终端,筛选程序可以运行并写入一个 .key 文件,但是当我尝试从我的程序中调用它时,没有任何内容被写入。 我是否正确使用了 exec() 方法?我查看了 API,但似
我在让 .getruntime.exec() 正常工作时遇到了一些问题。这是处理该部分的代码: while (line != null) { String name = line; Strin
我目前正在使用 Java 制作 Firefox 插件开发 GUI 工具。但是,当我尝试获取 .bat 的输出时,我陷入了困境。文件。 当我运行.bat时file using java 我可以看到输出,
我是一名优秀的程序员,十分优秀!