Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Java ZGC garbage collector USES a lot of memory

I built a simple application using Springboot.The ZGC garbage collector I use when deploying to a Linux server USES a lot of memory..I tried to limit the maximum heap memory to 500MB with Xmx500m, but the JAVA program still used more than 1GB. When I used the G1 collector, it only used 350MB.I don't know why, is this a BUG of JDK11?Or do I have a problem with my boot parameters? ####Runtime environment

  • operating system: CentOS Linux release 7.8.2003
  • JDK version: jdk11
  • springboot version: v2.3.0.RELEASE Here is my Java startup command
java -Xms128m -Xmx500m \
-XX:+UnlockExperimentalVMOptions -XX:+UseZGC \
-jar app.jar

Here is a screenshot of the memory usage at run time

Heap memory usage https://github.com/JoyfulAndSpeedyMan/assets/blob/master/2020-07-13%20201259.png?raw=true

System memory usage https://github.com/JoyfulAndSpeedyMan/assets/blob/master/2020-07-13%20201357.png?raw=true


Here's what happens when you use the default garbage collector Java startup command

java -Xms128m -Xmx500m \
-jar app.jar

Heap memory usage https://github.com/JoyfulAndSpeedyMan/assets/blob/master/2020-07-13%20202442.png?raw=true

System memory usage https://github.com/JoyfulAndSpeedyMan/assets/blob/master/2020-07-13%20202421.png?raw=true

By default jdk11 USES the G1 garbage collector. Theoretically, shouldn't G1 be more memory intensive than ZGC?Why didn't I use it that way?Did I misunderstand?Since I'm a beginner to the JVM, I don't understand why.

like image 276
greyWolf Avatar asked Jul 16 '20 02:07

greyWolf


People also ask

What is Zgc garbage collector?

The Z Garbage Collector (ZGC) is a scalable low latency garbage collector. ZGC performs all expensive work concurrently, without stopping the execution of application threads for more than 10ms, which makes is suitable for applications which require low latency and/or use a very large heap (multi-terabytes).

Does garbage collection use memory?

To improve performance, the runtime allocates memory for large objects in a separate heap. The garbage collector automatically releases the memory for large objects. However, to avoid moving large objects in memory, this memory is usually not compacted.

Is Zgc better than G1GC?

Shenandoah GC and ZGC perform significantly better than G1GC in terms of latency, average pause time, maxi- mum pause time, and total accumulated stop-the-world time, for all evaluated applications in the study when the right tunings are applied. It is possible to achieve more than 80% improvement on latency values.

What garbage collector does Java use?

Java garbage collection is an automatic process. Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects.


2 Answers

ZGC employs a technique known as colored pointers. The idea is to use some free bits in 64-bit pointers into the heap for embedded metadata. However, when dereferencing such pointers, these bits need to be masked, which implies some extra work for the JVM.

To avoid the overhead of masking pointers, ZGC involves multi-mapping technique. Multi-mapping is when multiple ranges of virtual memory are mapped to the same range of physical memory.

ZGC uses 3 views of Java heap ("marked0", "marked1", "remapped"), i.e. 3 different "colors" of heap pointers and 3 virtual memory mappings for the same heap.

As a consequence, the operating system may report 3x larger memory usage. For example, for a 512 MB heap, the reported committed memory may be as large as 1.5 GB, not counting memory besides the heap. Note: multi-mapping affects the reported used memory, but physically the heap will still use 512 MB in RAM. This sometimes leads to a funny effect that RSS of the process looks larger than the amount of physical RAM.

See also:

  • ZGC: A Scalable Low-Latency Garbage Collector by Per Lidén
  • Understanding Low Latency JVM GCs by Jean Philippe Bempel
like image 156
apangin Avatar answered Nov 10 '22 23:11

apangin


JVM uses much more than just the heap memory - read this excellent answer to understand JVM memory consumption better: Java using much more memory than heap size (or size correctly Docker memory limit)

You'll need to go beyond the heap inspection and use things like Native Memory Tracking to get a clearer picture.

I don't know what's the particular issue with your application, but ZGC is often mentioned as to be good for large heaps. It's also a brand new collector and got many changes recently - I'd upgrade to JDK 14 if you want to use it (see "Change Log" here: https://wiki.openjdk.java.net/display/zgc/Main)

like image 35
Juraj Martinka Avatar answered Nov 10 '22 23:11

Juraj Martinka