Java何时调用垃圾收集器 [英] When does Java calls Garbage Collector

查看:53
本文介绍了Java何时调用垃圾收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了许多有关Java Heap的文章.我看到如果出现java.lang.OutOfMemoryError,我们应该增加-Xmx来解决此问题.

I read many post about Java Heap. I saw if we get java.lang.OutOfMemoryError we should increase -Xmx to address this issue.

例如

java -Xmx2048m -Xms256m

这会将Java使用的堆内存增加到2048MB.现在出现了一个让我震惊的问题.

This will increase heap memory used by Java to 2048MB. Now comes to the question that is knocking on my head.

有关上述设置的问题:

在调用垃圾收集器之前,Java是否会等到其HEAP空间达到256MB?这是生产的原因,我们希望-XMx和-Xms具有相同的值,以便Java在达到最大内存分配之前不调用堆管理吗?

Does Java waits till its HEAP space reach to 256MB before calling garbage collector ? Is this the reason for production we want to have same value for -XMx and -Xms so that Java does not call Heap management before it reach maximum memory allocation ?

如果有人可以提供有关-Xmx和-Xms的其他详细信息,除了Xmx代表最大Java堆,而Xms代表最小Java堆,我将不胜感激.

I would appreciate if anyone can provide some additional details about -Xmx and -Xms beside Xmx represent max java heap and Xms represent min java heap.

推荐答案

第一个问题的答案是:取决于情况.您可以设置不同的GC策略,甚至可以说服JVM在堆满之前不运行GC,但这通常不是一个好主意.而且绝对不是通常发生的情况,而是将堆分为几个较小的堆用途不同的区域和垃圾收集器的填充会触发垃圾收集器.

The answer to the first question is: it depends. You can set different GC strategies and you can probably even convince the JVM to not run the GC until the heap is full but it's generally not a good idea. And it definitely isn't what typically happens, instead the heap is divided into several smaller areas of varying purpose and GC is triggered by their filling up.

要回答第二个问题:不,那是完全不同的问题.

To answer the second question: no, that's a completely different issue.

使用 -Xms256m 时,JVM将在启动时要求操作系统提供256 MB的内存.

With -Xms256m the JVM will ask the operating system for 256 megabytes of memory on startup.

如果它用完了,它将要求更多,直到达到 -Xmx 指定的数量.要求操作系统为进程分配更多的内存会花费一些时间,因此最好避免在服务器上使用,因为服务器几乎可以准确预测应用程序可以使用多少内存.

If it runs out of it, it will ask for more until it reaches the amount specified by -Xmx. Asking the OS to allocate your process more memory takes time and therefore is best avoided on a server, where you can predict almost exactly how much memory will be available for your application.

请注意,上述过程仅与GC间接相关,GC仅清除OS已经分配给JVM的堆.

Note that the above process is only indirectly related to the GC, which only clears heap that's already been assigned to the JVM by the OS.

要检查到底发生了什么,可以使用 -verbose:gc 命令行选项启动JVM.

To check what exactly is going on, you can start your JVM with the -verbose:gc command line option.

这篇关于Java何时调用垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆