简体   繁体   English

Runtime类中的内存方法是什么意思?

[英]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.

相关问题 在类中有更多方法意味着对象在运行时使用更多内存 - Does having more methods in a class mean that object uses more memory at runtime 类实例中的方法是否在内存中占有一席之地? - Do methods in class instances take a place in memory? java类类型 - 它们是什么意思? - java class types - what do they mean? Java 中的“运行时”是什么意思? - What does "runtime" mean in Java? 静态方法和字段是否在定义它们的类的实例中占用内存? - Do Static Methods and Fields take up memory in an instance of the class they are defined in? Java进程内存使用量远远超过Runtime.totalMemory等方法所说的 - Java process memory usage much more than what Runtime.totalMemory et al methods are telling “早期消费”是什么意思? 你的意思是memory在启动时消耗? - What does “early consumption” mean? Do you mean memory consumption at startup? 当他们说StringBuffer类已同步时,这到底是什么意思? 是否已声明该类的所有方法同步? - What exactly does it mean when they say that StringBuffer class is synchronized? Are all methods of that class declared synchronized? 类[] - 这是什么意思? - Class[] - What does it mean? java堆转储中的那些奇怪的类名是什么意思? - What do those strange class names in a java heap dump mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM