So I was wondering if there was any way to allow more ram to a program on a Raspberry Pi 3. I have a process which takes 76,7% of the memory (it's a tomcat server) and need another one like this to run, so my Raspberry can't handle it right now.

Is there a way to run a program using something else as ram or convert virtually a part of the sd card in ram (like on Android for instance). Or can we add physically a ram module ? Or can we force a program to take less memory ?

Thank you in advance !

  • There may be other RPi-like board that offer more ram. Here's a list of small -nix boards. raymii.org/s/articles/Small_Linux_PCs.html May 10 '17 at 14:44
  • "Is there a way to run a program using something else as ram or convert virtually a part of the sd card in ram (like on Android for instance)." -> Yes it is called swap, and it is probably the same mechanism as used in Android since they both use the Linux kernel. However, the SD card controller on the Pi is exceptionally slow compared to most modern smartphones, I think.
    – goldilocks
    May 10 '17 at 14:44
  • Your best bet is to get a server that does not eat up that much memory. Have you tried ngnix?
    – kwasmich
    May 10 '17 at 14:47
  • @kwasmich Mh it's actually an embedded Tomcat, so I don't really have the choice (it's a maven Alfresco app I'm trying to run)
    – Jack
    May 10 '17 at 14:51
  • @KevinKrooss thank you ! I'll check, but since I have a Raspberry I wanted to use it aha
    – Jack
    May 10 '17 at 14:52

I was wondering if there was any way to allow more ram

Not more physical RAM, but you can increase the amount of system memory by using swap, which is a region of secondary storage. Traditionally on linux this is done with a small dedicated partition on a hard disk. Current Raspbian uses something similar, a swap file, which is just a normal file that's created at boot.

This is done by an init service, dphys-swapfile (it's an old style SysV script in /etc/init.d), named after the executable of the same name, which is documented in man dphys-swapfile and configured in /etc/dphys-swapfile. Here's the stock Raspbian version:

# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
#   use under either modified/non-advertising BSD or GPL license

# this file is sourced with . so full normal sh syntax applies

# the default settings are added as commented out CONF_*=* lines

# where we want the swapfile to be, this is the default

# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation

# set size to computed value, this times RAM size, dynamically adapts,
#   guarantees that there is enough swap without wasting disk space on excess

# restrict size (computed and absolute!) to maximally this limit
#   can be set to empty for no limit, but beware of filled partitions!
#   this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
#   but is also sensible on 64bit to prevent filling /var or even / partition

Notice CONF_SWAPSIZE is uncommented, overriding the hardcoded default of "2 times RAM size". The number is in MiB. This makes sense on the Pi where the secondary storage is usually quite limited and creating a 1 or 2 GiB swap file would not be to many people's liking.

Obviously enough, you can change the number and reboot. You can then double check how much is available with free (or top, etc.).

I don't use swap on the pi but I imagine it is going to be noticeably slower. Ideally the system stashes the least used stuff in there. That your processes are java based may mitigate that -- I like to believe the JVM's colossal mem gluttony is because it preps large chunks ready for use (hence, they aren't just virtual, but resident), much of which isn't necessarily used very often.

If the file is to be large, I guess it would be nice to reuse it and not have it created at boot, which my reading of the docs implies is the normal case. I could be wrong about that; if not you may want to dig around and see if this can be overridden.

If the performance is too bad and you only need less than an extra GB, you could instead use a zram based swap, which is a compressed region of RAM. This used to be the default on Raspbian. I do not know what the ratio is, but if it were e.g., 2:1, and you used ~250 GB, you'd end up with 1.25 GB, 0.5 GB of it in zram.

Sample raspberry-pi 3 zram gist (I haven't tried this, it's from a suggested edit by another user)

  • Thank you for your detailed answer ! I'll check what you said on my Rasp and the net !
    – Jack
    May 10 '17 at 15:23
  • I added a last paragraph about zram. I'd try the swapfile first though and see how that works out.
    – goldilocks
    May 10 '17 at 15:32
  • Ok thank you ! I've put 1gio instead of 100Mo in the /etc/dphys-swapfile file and now I'm able to start the two tomcat servers ! Thank you a lot ! It's a bit slow but I guess on a Raspberry i couldn't expect a lot more aha
    – Jack
    May 11 '17 at 9:35
  • I've been searching for a way to install zram because it seems to be faster than swap but I can't find any repo, apt-get can't find the pckges. I've done this and this but it doesn't work. Do you know where I can find the right packages for a Raspberry Pi ?
    – Jack
    May 11 '17 at 14:20
  • There shouldn't be anything you need to install; swapon and mkswap are (pretty sure) part of the base, and the functionality is built into the kernel (more specifically, a module, zram.ko, but it is stock). Did you try the link in the last paragraph above (someone suggested that this morning)? If you can't get it to work, you should ask a separate question including details about what you tried and what happened.
    – goldilocks
    May 11 '17 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.