Java heap problems
cconde opened this issue · 1 comments
Hello,
Thanks for such a good work!
I'm receiving recently Java heap problems, and I'm unsure how to increase it with docker-compose.
There seems to be an option to limit it down, but I'm in the opposite. I want to boost the memory.
Thanks
Hey cconde,
Thank you so much for the kind words! Managing the memory for this can be quite tricky for sure.
So I've seen the heap error before. That is actually coming from Java. An example is here: https://jamesachambers.com/raspberry-pi-minecraft-server-script-with-startup-service/comment-page-45/#comment-14350
That one wasn't using the Docker container but since the error is actually a Java error the cause and the symptom are the same. The memory allocation issue comes up a lot more for the standalone version as it tends to be ran on devices with limited memory resources available.
Assuming your heap error is the same and ends with "Decrease heap size." then what this means is that you do not have as much (available) memory as you are telling Java to allocate.
Now you may be saying "but I do, I have that much memory!" and this is where things get tricky. You have to leave enough memory for the operating system and all of your other applications. You also have to leave room for Java (at least 100MB of RAM or so) which is not counted in the heap limit.
If you have 4GB of RAM for example it's only safe to allocate probably like 3GB of it since the OS and running services will typically be using at least something like 700MB-1GB of RAM on their own. You have to calculate this in because Java will not take either of those into account.
Basically Java is erroring out because the memory allocation you are giving it (plus the OpenJDK memory overhead + your OS and all other running applications) is going past the end of your available memory (so it crashes). It's basically a balancing act of leaving just enough for OpenJDK and your OS/applications.
You can get an idea of it with the server closed (assuming this is Linux) by running the 'free' command like:
ubuntu@jamesachambers:/var/www$ free
total used free shared buff/cache available
Mem: 4015088 1184776 1720680 90164 1109632 2487712
Swap: 0 0 0
This is my main web server and you can see that on my web server I'm using 1.1GB and have 2.4GB available. On this system I wouldn't want to try to set it to more than 2.4GB or it's going to crash with heap errors because that memory is already in use.
Realistically you'd also want to leave a safety margin because if the margin is razor thin then what will happen is that when your computer does something in the background like system updates for example that temporarily spikes the OS memory usage it will actually kill the Minecraft server (maybe even Docker entirely in this case) to reclaim the memory (so the entire OS doesn't crash).
Leaving it a nice margin will keep this from happening for sure. My standalone script is programmed to leave a nice fat safety margin and this isn't as much of a problem on there (but still comes up, the very post I linked to was my algorithm failing to leave enough of a safety margin creating this issue).
Hopefully that helps!