java - 查找Java中操作消耗的内存

标签 java memory-management

假设我在 Java 中做了一个像下面这样的冒泡排序示例:

package testing;

public class bubbleSort {
public static void main(String a[]) {
    int i;
    int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };
    System.out.println("Values Before the sort:\n");
    for (i = 0; i < array.length; i++)
        System.out.print(array[i] + "  ");
    System.out.println();
    bubble_srt(array, array.length);
    System.out.print("Values after the sort:\n");
    for (i = 0; i < array.length; i++)
        System.out.print(array[i] + "  ");
    System.out.println();
    System.out.println("PAUSE");
}

public static void bubble_srt(int a[], int n) {
    int i, j, t = 0;
    for (i = 0; i < n; i++) {
        for (j = 1; j < (n - i); j++) {
            if (a[j - 1] > a[j]) {
                t = a[j - 1];
                a[j - 1] = a[j];
                a[j] = t;
            }
        }
    }
}
}

有没有办法查出来

(a) 元素数组的数据结构消耗了多少 RAM?

(b) 如果不是 - 是否有办法比较该进程与普通 HelloWorld 相比消耗了多少 RAM?

package testing;

public class Testing {
public static void main(String[] args) {
    System.out.println("Hello World!");
}

}

最佳答案

How much RAM the data structure for the array of elements consumes?

不容易。它将有大约 40-48 个字节长,这不值得担心。

If not - is there a way to compare how much RAM that process consumes compared to a vanilla HelloWorld?

我猜测,您的第一个示例比第二个示例多使用了 100 KB。这是因为加载额外类并将 int 值转换为 String 的意义背后有 allot,这是大部分内存消耗。相比之下,您的阵列微不足道。

无论如何,100 KB 也不值得担心。在台式机中,100 KB 的成本不到 1 美分,并且可以重复使用。

关于java - 查找Java中操作消耗的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12176761/

相关文章:

c - C中的数据类型存储

c - 两次释放 glib 缓冲区是否安全?

java - SpringLayout 问题/挑战

java - 在 Android Studio 中导入 Facebook 库 : Could not find property 'ANDROID_BUILD_SDK_VERSION'

java - Spring Reactor 中的模拟服务

linux - 有没有基于线程的mprotect?

Java评分方法

Java this 和 super 关键字

objective-c - 分配/初始化 View 、添加到 subview 和返回的正确内存管理模式

c++ - 我可以使用 C++ 中的内置类型安全地新建 [],然后转换指针,然后删除 [] 吗?