我想我在 NetBeans 中将堆大小增加到了 1 GB,因为我将配置更改为如下所示:
netbeans_default_options="-J-Xmx1g ......
在我重新启动 NetBeans 之后,我可以确定我的应用程序现在有 1 GB 空间吗?
有没有办法验证这一点?
原文由 mrblah 发布,翻译遵循 CC BY-SA 4.0 许可协议
我想我在 NetBeans 中将堆大小增加到了 1 GB,因为我将配置更改为如下所示:
netbeans_default_options="-J-Xmx1g ......
在我重新启动 NetBeans 之后,我可以确定我的应用程序现在有 1 GB 空间吗?
有没有办法验证这一点?
原文由 mrblah 发布,翻译遵循 CC BY-SA 4.0 许可协议
public class CheckHeapSize {
public static void main(String[] args) {
long heapSize = Runtime.getRuntime().totalMemory();
// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
// Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();
System.out.println("heap size: " + formatSize(heapSize));
System.out.println("heap max size: " + formatSize(heapMaxSize));
System.out.println("heap free size: " + formatSize(heapFreeSize));
}
public static String formatSize(long v) {
if (v < 1024) return v + " B";
int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
return String.format("%.1f %sB", (double)v / (1L << (z*10)), " KMGTPE".charAt(z));
}
}
原文由 ram kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4.1k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
3 回答1.7k 阅读✓ 已解决
使用此代码:
知道它对我很有用。