ZGC used a lot of cache after startup

133 Views Asked by At

ZGC used alot of cache after startup even there is no request

OS: docker
jdk: openjdk version "17.0.8" 2023-07-18

This is the start command for my container

CMD ["java","-Xms1g","-Xmx1g","-XX:+UseZGC","-jar","/root/access-1.0.0.jar"]

The total_cache is too high

# cat /sys/fs/cgroup/memory/memory.stat
cache 1074008064
rss 185012224
rss_huge 67108864
shmem 1073741824
mapped_file 439083008
dirty 12288
writeback 0
swap 0
pgpgin 1096435
pgpgout 807453
pgfault 949371
pgmajfault 2
inactive_anon 1236013056
active_anon 12288
inactive_file 22716416
active_file 229376
unevictable 0
hierarchical_memory_limit 9223372036854771712
hierarchical_memsw_limit 9223372036854771712
total_cache 1074008064
total_rss 185012224
total_rss_huge 67108864
total_shmem 1073741824
total_mapped_file 439083008
total_dirty 12288
total_writeback 0
total_swap 0
total_pgpgin 1096435
total_pgpgout 807453
total_pgfault 949371
total_pgmajfault 2
total_inactive_anon 1236013056
total_active_anon 12288
total_inactive_file 22716416
total_active_file 229376
total_unevictable 0

But when I choose the g1 garbage collector, I don't have this problem

CMD ["java","-Xms1g","-Xmx1g","-jar","/root/access-1.0.0.jar"]
# cat /sys/fs/cgroup/memory/memory.stat
cache 53248
rss 620097536
rss_huge 360710144
shmem 0
mapped_file 32768
dirty 45056
writeback 0
swap 0
pgpgin 514950
pgpgout 451431
pgfault 537949
pgmajfault 0
inactive_anon 611667968
active_anon 8192
inactive_file 8093696
active_file 12288
unevictable 0
hierarchical_memory_limit 9223372036854771712
hierarchical_memsw_limit 9223372036854771712
total_cache 53248
total_rss 620081152
total_rss_huge 360710144
total_shmem 0
total_mapped_file 32768
total_dirty 45056
total_writeback 0
total_swap 0
total_pgpgin 514944
total_pgpgout 451431
total_pgfault 537942
total_pgmajfault 0
total_inactive_anon 611627008
total_active_anon 8192
total_inactive_file 8093696
total_active_file 12288
total_unevictable 0

I want to reduce the size of total_cache when using the zgc. Otherwise, my monitor will think that the pod's memory usage is too high and restart the pod.

1

There are 1 best solutions below

0
Stephen C On

ZGC uses a lot of cache after startup even there is no request.

So ... use G1GC. Or don't tell ZGC to use a 1GB initial heap size; i.e. remove the -Xms1g argument.

It is curious that G1GC and ZGC interpret the -Xms hint differently, but ZGC is actually doing what you told it to do. It is allocating RAM per your specified initial heap size.


Having said that, it is a known fact that Java uses more RAM than your main heap. RAM is used for the executable, for metaspace (containing bytecodes, JIT compiled code), for off-heap allocations such as direct buffers, and so on. So if you want to your pods being nuked too soon, you should configure the docker container with significantly larger RAM limits than the Java max heap size, or reduce the heap size.

How much extra RAM is consumed will depend on your Java application. I can't suggest anything better than trial and error for determining a "good" RAM limit or heap size.