-1

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • It's probably the operating system of your raspberry which prevents swapping. Because swapping a lot data on a sd-card is just stupid. If you really want swap, attach a real usb hard-disk to your PI and create a swap file/partition onto that disk. But if you ask me, just don't use java on a PI, at least not that big size java app, lol. – paladin Nov 18 '20 at 07:32
  • @paladin my swap file is already on a usb flash drive, i just need more ram, i accept the consequences – James B. Reese Nov 18 '20 at 07:38
  • Java just asks the operating system for memory for the heap, it is up to the OS to use the swap. Is this a 32 bit Java? Around 2Gb is usually the maximum memory a 32 bit Java can use, it needs to be 64 bit to use more. – greg-449 Nov 18 '20 at 08:20
  • @greg-449 it fails even if i set -Xmx to 1900M – James B. Reese Nov 18 '20 at 08:26
  • Just to make sure; are you using a 64bit OS? – paladin Nov 18 '20 at 08:29
  • no, the os is 32 bit, as it is the only one which is officially supported, though i can do -Xmx1300 to use more than a gig. – James B. Reese Nov 18 '20 at 08:32
  • This is mostly the reason why you cannot allocate that much memory, 32bit has its limits, and a 2gibibyte (or 4gibibyte) barrier is one of them. Use a Debian 64bit distribution for your Raspberry Pi, this might work. But you have to maintain many other parts for your Raspberry by yourself, because Debian doesn't include all third party programs of Raspbian OS. – paladin Nov 18 '20 at 08:41
  • *"it fails even if i set -Xmx to 1900M"* - What is the error message? – Stephen C Nov 18 '20 at 08:44
  • Of course it fails, because the Raspberry OS also needs memory. It also uses "hidden memory addresses" for hardware. You just cannot allocate that much memory on a Raspberry PI with 32bit OS. Deal with it. – paladin Nov 18 '20 at 08:46

1 Answers1

2

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. (See man 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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I already have swap set up correctly, my problem is that java does not see it. – James B. Reese Nov 18 '20 at 07:44
  • Please verify that your swap works as intended, raspberry OS has often "different" settings for swap enabled, which are actually deactivating swap, even if a swap file/partition is configured. – paladin Nov 18 '20 at 07:46
  • Yes, it works as intended. I have stress tested the ram and swap gets used – James B. Reese Nov 18 '20 at 07:47
  • 3
    *"I already have swap set up correctly, my problem is that java does not see it. "* - You **don't** have it configured correctly. If you did then Java **would** see it. Sorry, but Java doesn't have any way to configure swap space. It is not its concern. It is not something that a non-privileged application can even configure. – Stephen C Nov 18 '20 at 07:48