且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JVM进程大小和内存堆大小之间的巨大差异

更新时间:2022-10-17 21:05:09

虚拟内存分配大多无关紧要(参见解释这个答案)与实际内存使用情况大不相同。 JVM不是为限制虚拟内存分配而设计的,请参阅有关限制虚拟内存使用情况的问题。

最终用户可能会在任务管理器中看到大量的虚拟内存使用情况,但这几乎毫无意义。 Windows任务管理器中显示的内存使用的不同数字在本文。总结:在Windows任务管理器中查看内存(私有工作集)和页面错误增量(后者的相关性在这个答案)。


I am developing java swing application on Windows 8.1 64bit with 4GB RAM with JDK version 8u20 64bit.

The problem is when I launched the application with Netbeans profiler with Monitor option.

When the first Jframe is loaded the application Memory Heap is around 18mb and the JVM process size around 50mb (image1).

Then when I launch the other Jframe which contains a JFxPanel with webView the Heap jumps to 45mb and the JVM proccess jumps to 700mb very fast (image2) which is very confusing. Then when I close the second JFrame and it get disposed and a System.gc() is called and the JVM perform a GC (in most times) the heap drops to around 20mb but the JVM proccess never drops (image3).

Why there is a huge difference between memory Heap (45 Mb) and JVM proccess (699 Mb)? Why the JVM needs all that Memory? And how to reduce that amount? And I am launching the app with those vm options:

-Xms10m  -Xmx60m -Xss192k
-XX:+UseG1GC -XX:MinHeapFreeRatio=5
-XX:MaxHeapFreeRatio=10  -XX:PermSize=20m
-XX:MaxPermSize=32m  

EDIT :- I just read the question in that link JVM memory usage out of control and he have the same problem but the situation is different his heap size is arround 33% of the total JVM process memory size which is in my case less than 7% and he is doing multiple jobs simultaneously (Tomcat webapp) which I don't (java swing application) and he didn't launch his application with the same VM arguments I did.

UPDATE :- after the first JFrame is launched (image1)

after the second JFrame is launched (image2)

after the second JFrame is closed (image3)

EDIT 2 :- I just tried the same application with the same VM arguments above and added

-client 
-XX:+UseCompressedOops 

and used JDK 8u25 32-bit because as mentioned in this answer https://***.com/a/15471505/4231826 the 64-bit version doesn't include client folder in the JRE and will ignore -client argument.

The result is that the total memory process jumped to 540Mb when the second JFrame was open and the heap sizes (in the the three points) were almost the same numbers as in the 64-bit version, Does this confirm that this is a problem related to the JVM (The same heap sizes and a 260Mb difference in total process sizes)?

Virtual memory allocation is mostly irrelevant (see for an explanation this answer) and very different from actual memory usage. The JVM is not designed to limit virtual memory allocation, see the question about limiting virtual memory usage.
The end-user may see a lot of virtual memory usage in the task-manager, but that is mostly meaningless. The different numbers for memory usage shown in the Windows task manager are explained in this article. In summary: in the Windows task manager look at the "Memory (Private Working Set)" and "Page Fault Delta" (the relevance of the latter is explained in this answer).