java - 如何从应用程序中准确找到 Java 应用程序使用了多少内存

标签 java memory memory-management runtime usage-statistics

<分区>

我正在开发一个仅用 Java 编写的应用程序,该应用程序通常会使用大量数据(最多 500 GB)。该程序将保存当前未在数据库中使用的数据。

但是,我想在应用程序中保留尽可能多的数据,因为很多数据经常被重用。为了不出现任何内存不足错误,我希望能够在任何时刻检查整个应用程序使用了多少内存,以便在应用程序接近预定义限制时存储它。

类似这样的东西;

long memoryUsage = SomeClass.getCurrentMemoryUsage();

最佳答案

如果您专门在 JVM 内存中查找:

Runtime runtime = Runtime.getRuntime();

NumberFormat format = NumberFormat.getInstance();


StringBuilder sb = new StringBuilder();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();

sb.append("free memory: " + format.format(freeMemory / 1024) + "<br`enter code here`/>");   sb.append("allocated memory: " + format.format(allocatedMemory / 1024) + "<br/>");
sb.append("max memory: " + format.format(maxMemory / 1024) + "<br/>");
sb.append("total free memory: " + format.format((freeMemory + (maxMemory - 
allocatedMemory)) / 1024) + "<br/>");

关于java - 如何从应用程序中准确找到 Java 应用程序使用了多少内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650561/

相关文章:

java - 单线程 Flux 中的 Mono

Java5-XX :MaxHeapFreeRatio=45 doesn't release heap even when more than 45% of the heap is free

java - 一个类实现两个或多个接口(interface)是一个好习惯吗?

java - 类加载期间的内存分配

iphone - 如何推送 View Controller ( View Controller )?

java - Double 上的数学运算结果是否完全可重复?

java - Apache Spark : ERROR local class incompatible when initiating a SparkContext class

java - 为什么不使用静态字符串数组?

opencv - 是否可以在OpenCV的特定图像上存储数据?

numpy - 当使用自身的部分 View 覆盖数组时,Numpy 的内存管理是什么?