question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set Scala kernel JVM max heap size based on the environment variables

See original GitHub issue

I am running my Jupyterhub in a Docker container on Kubernetes. The Almond kernel starts as JVM process in that container. The process call for the kernel looks like following there

java -jar /root/.local/share/jupyter/kernels/scala/launcher.jar --connection-file /root/.local/share/jupyter/runtime/kernel-b930d107-f417-4580-9ca9-2dc63e5a26e4.json

As you can see, no Java heap parameters get passed to the process. According to the JVM logic the Java heap max size is set to 1/4 of the available RAM. In a containerized environment, the available RAM is the full RAM of the host/machine where the container is running, it is 16GB in my case. So the Scala kernel gets 4GB max heap size. Since I am running Jupyterhub in a container on Kubernetes, I have to set some reasonable memory limits for my pod. If I set it to anything lower than 4GB the kernel process will crash with OOM and get automatically restarted.

I’ve read this article about how I can pass JVM args to the Scala kernel. But in my case during kernel installation into my container image I don’t know the max heap size, that I will want to have at runtime. So I cannot really hardcode some fixed max heap size into my image.

It would be great to have a mechanism to be able to set Scala kernel max heap (and other JVM parameters) from environment variables. Something like JAVA_OPTS used in Apache Tomcat for example.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
alexarchambaultcommented, Apr 8, 2019

@sbrunk I’m going to add support for reading JAVA_OPTS in bootstraps. (It should have been added long ago… Edit: done, https://github.com/coursier/coursier/pull/1118, should be included in the next coursier release)

About launching launcher.jar or launcher.bat directly, this would require copying the launcher bat file along its jar on Windows (or generating our own bat file).

1reaction
sbrunkcommented, Feb 22, 2019

Respecting container RAM limits has been backported to Java 8 as well. Since update 131 or so, it works if you add the the -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap JVM args. You should also add -XX:MaxRAMFraction=2 to use half of the available memory instead of 1/4. It’s still wasting memory though. Don’t be tempted to set -XX:MaxRAMFraction=1 as it will try to use all available container memory for the JVM which will get your pod killed.

Since update 191, the experimental -XX:+UseCGroupMemoryLimitForHeap parameter has been replaced by -XX:+UseContainerSupport which is enabled by default. Even better, it adds -XX:MaxRAMPercentage as a replacement for -XX:MaxRAMFraction which allows more fine control over how much memory the JVM gets.

I.e. use -XX:MaxRAMPercentage=75.0 depending on what else allocates memory in the container.

The openjdk:8-jre-alpine is already at u191 while openjdk:8-jre-slim seems to be still at u181 at the moment.

See also the discussion in this SO thread.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set Scala kernel JVM max heap size based on the environment ...
Set Scala kernel JVM max heap size based on the environment variables.
Read more >
How do I set Java's min and max heap size through ...
The maximum heap size you can set depends entirely on the environment: for most 32-bit systems, the maximum amount of heap space you...
Read more >
Environment variables for sizing NameNode heap memory
HADOOP_HEAPSIZE sets the JVM heap size for all Hadoop project servers such ... HADOOP_HEAPSIZE is an integer passed to the JVM as the...
Read more >
JVM Configuration - Humio Documentation
This sets Humio to allocate a heap size of 16 GB and further allocates 16 GB for direct memory access (which is used...
Read more >
Configuring Heap Size for SBT | Baeldung on Scala
In this way, we can adjust this value to our needs. We have also specified the –Xms option which tells the JVM what's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found