When does Java calls Garbage Collector

落爺英雄遲暮 提交于 2021-02-07 10:27:12

问题


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

for e.g

java -Xmx2048m -Xms256m

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

Question with reference to above settings :

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 ?

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.


回答1:


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.

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

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.

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.

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




回答2:


Garbage collection occurs when the generations fill up.

You have two primary generations on the heap: young and old. The young generation is also known as the new generation, or eden space. The old generation is subdivided into survivor space and tenured generation.

Newly allocated objects start in the young generation, and are moved into the old generation if they survive garbage collection. Garbage collection runs more frequently in the young generation.

These are your Heap Tuning Parameters:

  • -Xmsvalue (initial/minimum heap size, all generations)

  • -Xmxvalue (maximum heap size, all generations)

  • -Xmnvalue (new generation size, alternative for -XX:NewSize below)

  • -XX:MinHeapFreeRatio=minimum (minimum desired proportion of free space to living objects, as a percentage)

  • -XX:MaxHeapFreeRatio=maximum (maximum desired proportion of free space to living objects, as a percentage)

  • -XX:NewRatio=ratio (ratio of old generation to new generation: default is 2, meaning 2:1, i.e. the heap is 2/3 old generation)

  • -XX:NewSize=size (initial/minimum new generation size)

  • -XX:MaxNewSize=size (maximum new generation size)

  • -XX:+AggressiveHeap (size of initial heap is calculated based on the size of the physical memory and attempts to make maximal use of the physical memory for the heap)




回答3:


-Xms : min heap size. Having -Xms = 1.8GB (32bit) can be bad, because you don't let memory for anything else.

-Xmx : max heap size (ex: -Xmx1024)

See here for more: Get started with java JVM memory (heap, stack, -xss -xms -xmx -xmn...)



来源:https://stackoverflow.com/questions/28116708/when-does-java-calls-garbage-collector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!