Cloud Native Java

61 阅读4分钟

In container you still need log library for logging, not use system.out/err.

The Java runtime is a fully self-reliant system, designed to run and improve code on a single system. The runtime works through Just In Time (JIT) compilation, using local resources that convert Java bytecode ( JAR and class files) to native machine code to improve the speed and memory. But that need much more resoures overload according to cloud native environment. see below,
Azul Launches Java Cloud Compiler

Scaling Java vertically requires proper JVM configurations, and the shrinking garbage collector
Cost Reduction Strategies on Java Cloud Hosting Services Java and Memory Limits in Containers: LXC, Docker and OpenVZ Memory inside Linux containers

How to do JVM setting in containers?

In the past, the JVM was not aware of the memory and CPU allocated to the container. So, Java 10 introduced a new setting: +UseContainerSupport (enabled by default) to fix the root cause, and developers backported the fix to Java 8 in 8u191. The JVM now calculates its memory based on the memory allocated to the container. A lot has changed since Java 8u191 and things should work out of the box.
How To Configure Java Heap Size Inside a Docker Container Java Heap Sizing in a Container: Quickly and Easily JVM Memory Settings in a Container Environment

  1. if not set -Xmx and -Xmx parameters, the default JVM sizes the heap based on the system, usually is 1/4 of the system memory.
  2. it's better set -Xmx and -Xmx parameters always.

for old deprecated setting in containers, see Java Memory Arguments for Containers

OOM Error Analysis In Containers

OOMkillers in Docker are more complex than you thought

Why does my Java process consume more memory than Xmx?

Javamemory (maxram) = metadata + thread + code cache + offline + heap

how to check container memory usage?

check /sys/fs/cgroup directory in the pod, like this,

bash-4.2@pro# ls -l /sys/fs/cgroup/
total 0
drwxr-xr-x 2 root root  0 Jul 21 10:51 blkio
lrwxrwxrwx 1 root root 11 Jul 21 10:51 cpu -> cpu,cpuacct
lrwxrwxrwx 1 root root 11 Jul 21 10:51 cpuacct -> cpu,cpuacct
drwxr-xr-x 2 root root  0 Jul 21 10:51 cpu,cpuacct
drwxr-xr-x 2 root root  0 Jul 21 10:51 cpuset
drwxr-xr-x 2 root root  0 Jul 21 10:51 devices
drwxr-xr-x 2 root root  0 Jul 21 10:51 freezer
drwxr-xr-x 2 root root  0 Jul 21 10:51 hugetlb
drwxr-xr-x 2 root root  0 Jul 21 10:51 memory
lrwxrwxrwx 1 root root 16 Jul 21 10:51 net_cls -> net_cls,net_prio
drwxr-xr-x 2 root root  0 Jul 21 10:51 net_cls,net_prio
lrwxrwxrwx 1 root root 16 Jul 21 10:51 net_prio -> net_cls,net_prio
drwxr-xr-x 2 root root  0 Jul 21 10:51 perf_event
drwxr-xr-x 2 root root  0 Jul 21 10:51 pids
dr-xr-xr-x 2 root root  0 Jan 20 02:53 rdma
drwxr-xr-x 2 root root  0 Jul 21 10:51 systemd

bash-4.2@pro# ls -l /sys/fs/cgroup/memory/
total 0
-rw-r--r-- 1 root root 0 Jul 21 10:51 cgroup.clone_children
--w--w--w- 1 root root 0 Jul 21 10:51 cgroup.event_control
-rw-r--r-- 1 root root 0 Jul 21 10:51 cgroup.procs
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.failcnt
--w------- 1 root root 0 Jul 21 10:51 memory.force_empty
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.failcnt
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.limit_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.max_usage_in_bytes
-r--r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.slabinfo
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.tcp.failcnt
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.tcp.limit_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.tcp.max_usage_in_bytes
-r--r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.tcp.usage_in_bytes
-r--r--r-- 1 root root 0 Jul 21 10:51 memory.kmem.usage_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.limit_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.max_usage_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.memsw.failcnt
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.memsw.limit_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.memsw.max_usage_in_bytes
-r--r--r-- 1 root root 0 Jul 21 10:51 memory.memsw.usage_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.move_charge_at_immigrate
-r--r--r-- 1 root root 0 Jul 21 10:51 memory.numa_stat
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.oom_control
---------- 1 root root 0 Jul 21 10:51 memory.pressure_level
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.soft_limit_in_bytes
-r--r--r-- 1 root root 0 Jul 21 10:51 memory.stat
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.swappiness
-r--r--r-- 1 root root 0 Jul 21 10:51 memory.usage_in_bytes
-rw-r--r-- 1 root root 0 Jul 21 10:51 memory.use_hierarchy
-rw-r--r-- 1 root root 0 Jul 21 10:51 notify_on_release
-rw-r--r-- 1 root root 0 Jul 21 10:51 tasks

bash-4.2@pro# cat /sys/fs/cgroup/memory/memory.stat
cache 17924096
rss 1961607168
rss_huge 1400897536
shmem 0
mapped_file 32768
dirty 90112
writeback 0
swap 0
pgpgin 235668
pgpgout 93732
pgfault 1511226
pgmajfault 0
inactive_anon 0
active_anon 1961541632
inactive_file 17920000
active_file 0
unevictable 0
hierarchical_memory_limit 8589934592
hierarchical_memsw_limit 8589934592
total_cache 17924096
total_rss 1961607168
total_rss_huge 1400897536
total_shmem 0
total_mapped_file 32768
total_dirty 90112
total_writeback 0
total_swap 0
total_pgpgin 235668
total_pgpgout 93732
total_pgfault 1511226
total_pgmajfault 0
total_inactive_anon 0
total_active_anon 1961541632
total_inactive_file 17920000
total_active_file 0
total_unevictable 0