Nexus Docker container eating up all of my hosts's memory?

Nexus v 3.32.0-03

I have a dedicated Linux Ubuntu host with 32G memory running my Nexus server, which I configured to run as a Docker container, which I start this way.

docker run --rm -d -p 8081:8081 --memory 16g --name nexus -v nexus-data:/nexus-data sonatype/nexus3

For some reason, it just eats up all my systems’s memory despite of the --memory 16g flag. I restarted my host last night, and today I see this. htop shows a bunch of PIDs from user 200 that use 5G memory.

$ free -g
              total        used        free      shared  buff/cache   available
Mem:             31          22           5           0           3           8
Swap:            39           0          39

What can I do to prevent this? Thanks!

I’ll try this from Docker Hub

docker run --rm -d -p 8081:8081 -e INSTALL4J_ADD_VM_PARAMS="-Xms6G -Xmx6G -XX:MaxDirectMemorySize=15530M" --name nexus -v nexus-data:/nexus-data sonatype/nexus3

That didn’t help.