Java container crashes out of memory in kubernetes

2/20/2018

This container runs fine on Docker on my laptop, but crashes with java.lang.OutOfMemoryError: Java heap space when run in Kubernetes. Why?

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mem-eater
spec:
  template:
    metadata:
      labels:
        # Refer to this name when defining a service.
        app: mem-eater
    spec:
      containers:
        # This is the docker image on your docker registry. This one is on
        # the official Docker registry.
      - image: neilhwatson/memory-eater:prod
        name: mem-eater

It tried setting resource limits and requests, and JVM Xmx limits, but in all cases the crash is exactly the same.

Other info:

  • Using Java 9 64bit
  • k8s 1.8.8 installed on AWS using kops.
  • Docker CE 17.12
-- Neil H Watson
kubernetes

2 Answers

2/20/2018

Have you tried the -XX:+UseCGroupMemoryLimitForHeap option? It tells the JVM to use the cgroup-provided limits for determining heap sizes (and other resource limits) rather than the system-wide limits.

Christine Flood wrote a great article on some flags that are immensely useful when using Java on containers: https://developers.redhat.com/blog/2017/04/04/openjdk-and-containers/

-- omajid
Source: StackOverflow

2/20/2018

I have the exact opposite situation, crashing with

docker run -it neilhwatson/memory-eater:prod

while in Kubernetes runs fine, no restarts (but after 511 iterations stops logging).

(sorry for not commenting, I don't have enough rep).

-- Alessandro Vozza
Source: StackOverflow