问题
So, I have a java jar to run on my raspberry pi, and it requires a lot of memory. I have set up 2 gb swap on an external usb flash drive. But when I start the jar with -Xmx2048M, it says that it could not reserve enough space for 2097152KB object heap. I know this is a bad idea, but I want to do it anyway. How do I go about making java recognize the swap as heap space?
回答1:
Java doesn't know about swap space. It just asks the OS for "more memory", and the OS says "yes" or "no" depending on what is still available.
Swap space is configured at the Raspberry Pi operating system level; see How to set up swap space for details.
But have you tried the normal things?
- Use the
-Xmx
JVM command line option to set the Java heap size1. (Seeman java
for the details.) - Check that there isn't a per-process "ulimit" that is stopping the JVM process from asking for more memory. (See
man ulimit
for the details.)
1 - This is not "telling it to use swap". It is telling Java to set the maximum heap size to a value that is different to the default max heap size.
UPDATE - I see from a comment that you know about the -Xmx option, and are presumably using it. So that leaves one more thing. Since you are using a 32-bit JVM on a 32-bit OS, there is a architectural limit on the JVM's total memory usage. The limit will be less than 4 GB ... all up. (According to this, the limit is 3.6 GB or thereabouts, though I am not convinced that is accurate.)
If you are running up against that limit, there is nothing you can do about it, I'm afraid.
You say:
it fails even if I set -Xmx to 1900M
What is the complete error / exception message?
I just need more ram.
Well, if you literally need more RAM, you need to buy a Raspberry Pi with more RAM. (Apparently, there aren't any supported options for expanding the RAM on an existing Pi.)
Increasing the RAM and increasing the available virtual memory are NOT the same thing ...
Note that if you size a JVM's heap to be larger than the available RAM, you are liable to run into problems with thrashing when the JVM does a full garbage collection.
来源:https://stackoverflow.com/questions/64888598/make-java-understand-that-swap-exists-and-encourage-him-to-use-it