[英]What do the memory methods in the Runtime class mean?
Runtime.getRuntime().totalMemory()
I imagine this is the amount of memory I am currently using - the amount of allocated memory. 我想这就是我当前正在使用的内存量-已分配的内存量。 More objects, more memory, yes?
更多的对象,更多的内存,是吗?
Runtime.getRuntime().freeMemory()
And this would be, huh, the amount of not-allocated memory I can use? 这就是我可以使用的未分配内存量吗?
Therefore, the TOTAL amount of memory available to my program should be totalMemory() + freeMemory()
? 因此,我的程序可用的总内存量应该是
totalMemory() + freeMemory()
? The naming convention doesn't help me much. 命名约定对我没有太大帮助。
But then, what is this 但是那是什么
Runtime.getRuntime().maxMemory()
exactly? 究竟?
Javadoc indeed give a very clear definition. Javadoc确实给出了非常明确的定义。 However it seems to me that OP is a bit confused on how JVM works.
但是在我看来,OP对JVM的工作方式有点困惑。
First, definition from Javadoc: 首先,来自Javadoc的定义:
freeMemory() : Returns the amount of free memory in the Java Virtual Machine.
freeMemory():返回Java虚拟机中的可用内存量。
maxMemory() : Returns the maximum amount of memory that the Java virtual machine will attempt to use.maxMemory():返回Java虚拟机将尝试使用的最大内存量。
totalMemory() : Returns the total amount of memory in the Java virtual machine.totalMemory():返回Java虚拟机中的内存总量。
Explanation on how JVM allocate memory (this is of course a very brief explanation but it should give you the picture): 关于JVM如何分配内存的说明(这当然是一个非常简短的说明,但是应该可以为您提供图片):
In most, if not all, JVM implementation, there is usually a upper limit of memory that a JVM can request from OS. 在大多数(如果不是全部)JVM实现中,JVM通常可以从操作系统请求内存的上限。 (eg in Sun/Oracle JVM, it is affected by
-Xmx
). (例如,在Sun / Oracle JVM中,它受
-Xmx
影响)。 If you set the limit to 2GB, then maxMemory()
is going to return you 2GB. 如果将限制设置为2GB,则
maxMemory()
将返回2GB。
However, a JVM start, it is allowed to allocate less memory than its allowed upper limit. 但是,在JVM启动时,允许分配的内存少于其允许的上限。 For example, even you have set the upper limit to 2GB, when the JVM start, it can request for only 500MB from OS.
例如,即使您将上限设置为2GB,当JVM启动时,它也只能从OS请求500MB。 When there is more and more object created, and when it need more memory, it can then request more memory from the OS, until it hit the upper limit.
当创建的对象越来越多,并且需要更多的内存时,它可以从OS请求更多的内存,直到达到上限。
In the above case, when JVM has allocated only 500MB from OS, totalMemory()
is going to return 500MB. 在上述情况下,当JVM仅从OS分配了500MB时,
totalMemory()
将返回500MB。
If within that 500MB, 400MB is already used (for objects created etc), then freeMemory()
will return you 100MB. 如果在那500MB之内,已经使用了400MB(用于创建的对象等),那么
freeMemory()
将返回100MB。
I believe this is the Maximum menory allocated to your Java programm, if you start your program from Console, you can specify the max memory ( java -Xms
& -Xmx
). 我相信这是分配给Java程序的最大内存,如果从控制台启动程序,则可以指定最大内存(
java -Xms
和-Xmx
)。 Example would be java -jar -Xms1024MB -Xmx1024MB myjar.jar
. 示例为
java -jar -Xms1024MB -Xmx1024MB myjar.jar
。
How it helps :D 它如何帮助:D
Total memory is the current amount of memory managed by the virtual machine. 总内存是虚拟机管理的当前内存量。 This does not reflect how much is actually used, but how much is managed.
这并不反映实际使用了多少,而是管理了多少。 Free memory is the amount of total memory that is currently free.
可用内存是当前可用的总内存量。 The amount used would roughly be total - free.
所使用的金额大致是总计-免费。 Max memory is the max that the virtual machine would ever attempt to grow itself to (ie Xmx).
最大内存是虚拟机尝试将自身增长到的最大内存(即Xmx)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.