简体   繁体   English

Java阅读Ram用法

[英]Java Reading Ram Usage

When i want to read ram usage from my program with 当我想从我的程序中读取ram的用法时

"long used = Runtime.getRuntime().totalMemory() -Runtime.getRuntime().freeMemory();" “长时间使用= Runtime.getRuntime()。totalMemory()-Runtime.getRuntime()。freeMemory();”

method, it returns me the same value every time. 方法,每次都会返回相同的值。 For example my code 例如我的代码

if(selectSort.equals("1")){

                //-------------------------BEST CASE MERGE---------------------------------------

                if(selectArray.equals("1"))
                    mergeSort.mergeSort(array1,0,array1.length - 1);

                else if(selectArray.equals("2"))
                    mergeSort.mergeSort(array2,0,array2.length - 1);

                else if(selectArray.equals("3"))
                    mergeSort.mergeSort(array3,0,array3.length - 1);

                else if(selectArray.equals("4"))
                    mergeSort.mergeSort(array4,0,array4.length - 1);
                else
                    System.out.println("Wrong Choice");

                long used = Runtime.getRuntime().totalMemory() -Runtime.getRuntime().freeMemory();
                String memoryString = format.format(used / (1024*1024));
                System.out.println("\nMemory Usage of : " + memoryString + " MB");

                //-----------------------------END---------------------------------------------------   
            }

In here there is no problem, ram values are 6 - 7 - 17 - 27 in an order BUT 在这里没有问题,按BUT顺序ram值是6-7-17-27

if(selectSort.equals("2")){ 

                //-------------------------BEST CASE QUICK---------------------------------------

                if(selectArray.equals("1"))
                    quickSort.quickSort(array1, 0, array1.length - 1);
                else if(selectArray.equals("2"))
                    quickSort.quickSort(array2, 0, array2.length - 1);
                else if(selectArray.equals("3"))
                    quickSort.quickSort(array3, 0, array3.length - 1);
                else if(selectArray.equals("4"))
                    quickSort.quickSort(array4, 0, array4.length - 1);
                else
                    System.out.println("Wrong Choice");

                long used = Runtime.getRuntime().totalMemory() -Runtime.getRuntime().freeMemory();
                String memoryString = format.format(used / (1024*1024));
                System.out.println("\nMemory Usage of : " + memoryString + " MB");

In there ram values are the same, 6. I cannot understand why is it? 其中的ram值是相同的6.我不明白为什么? Can you help me? 你能帮助我吗? Thanks. 谢谢。

Here is my program, just import it and start, there is a menu ;) 这是我的程序,只需将其导入并启动,就有一个菜单;)

https://drive.google.com/open?id=0B-4t0pVRkFn_b3FJQkF0cmFIWlE https://drive.google.com/open?id=0B-4t0pVRkFn_b3FJQkF0cmFIWLE

What these calls are measuring is the allocation of memory of the JVM. 这些调用所测量的是JVM的内存分配。 There is an important difference between the amount of memory that the OS has allocated for the JVM and the amount of that memory that the JVM actually uses to store data. OS为JVM分配的内存量与JVM实际用于存储数据的内存量之间存在重要区别。

Furthermore, JVM internal memory is only freed after the garbage collector has run (GC). 此外,仅在运行垃圾收集器(GC)之后才释放JVM内部内存。 And after that the memory is usually not returned to the OS. 之后,通常不会将内存返回给操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM