Java Runtime.maxMemory incorrect?
I ran the following method Runtime.getRuntime().maxMemory() and gave 85196800.
However, I then ran top from the command line and it showed
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 开发者_开发百科
8672 root 20 0 1284m 156m 4296 S 0.3 60.9 0:33.35 java
Doesn't that show 156M of ram used? Any ideas what is going on?
From the documentation,
maxMemory() - Returns the maximum amount of memory that the Java virtual machine will attempt to use.
Top only shows the amount of (virtual) memory that the system has allocated to the process - you are asking Java how much it could attempt to use in the worst case.
In general, querying the JVM and/or system for information on actual memory used is not reliable. For example, top's numbers may include memory allocated but not used or paged out. It can also include things like shared libraries, where a 10MB library may count for two processes' allocations but only have one physical copy in memory. (For example)
What are you trying to do?
The Javadocs for that method are wrong, or at least very misleading. This Sun bug report explains.
The other point is that the 156Mb shown as RES is the current "resident set" size; i.e. the amount of physical RAM currently attributed to the application. This number is liable to increase and decrease depending on the virtual memory demands of the system services / daemons and applications running on the machine. The numbers that the JVM purports to report are the JVM's virtual memory allocation.
Suffice it to say that this is all as clear as mud, and probably not worth your effort to try to figure it out. If you really care, pay attention to what top
, vmstat
and so on tell you, and ignore the JVM numbers.
Runtime.getRuntime().maxMemory() is returning an estimate of the maximum heap size, not the total amount of memory that will be consumed by the entire process. Native memory, native libraries, etc. will contribute to the process size but not to maxMemory().
if you are trying to understand why top shows more memory consumed than set in jvm .
This is my experience in the past
We have seen TOP and other similar utilities showing more memory consumed than we originally set in JVM params .
In our case the environment is : AIX / Websphere Application server .
The reason was : We had one Shared library defined in Classpath which we use in our application . This particular Library (PureedgeAPI) actually spans another process or makes a JNI Call . (Few .SO libraries for clarity ) After we few incorrect calls - i mean without destroying by calling this objects.destroy() not just setting it to NULL. we have seen memory raising to the level of 30GB on jvm with 2gb (max set)
From OS perspective : since JAVA was the original process - all child instances of this JNI call were also recorded as its the same JAVA parent process which is using it .
I do not have commands handy - but i would actually see the tree of this particular process (this might have spawned another process (in your case java ) ).
Thank you,
精彩评论