Page 1 of 1

Use SD card for memory

Posted: Wed Jul 17, 2019 3:43 pm
by tldr6501
I was just told you can use an SD card to increase the memory. Is this correct?

Re: Use SD card for memory

Posted: Wed Jul 17, 2019 7:43 pm
by neilgl
No. The memory on the pi is the RAM. e.g 1GB, or up to 4GB on a pi4. The microSD card is the storage e.g. 32GB or 64GB or larger.

Re: Use SD card for memory

Posted: Wed Jul 17, 2019 8:05 pm
by Andyroo
Raspbian supports a swap file where memory is written out to the SD card temporarily to free up physical memory.

In the rare case you get errors such as
Cannot allocate memory
then you may need to edit the amount of swap space allocated.

The issues are that this can kill the SD card due to ware levelling and is slower than the on board memory.

You can see how much memory you are using by the command

Code: Select all

free -m
and you can change the value by changing the swap size value as follows

Code: Select all

sudo nano /etc/dphys-swapfile
CONF_SWAPSIZE=xxxx
Then issue
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
In most cases you do not need to do this - if you are running a hard disk via USB it’s definitely worth it though esp. if you are running the GUI and a few Chromium tabs.

Do a Google on SD wear and dphys-swapfile

Note: Above does still apply to the older boards where memory was limited to 256mb but you may have a better use case for doing this :lol: None of my Zeros have this and the only boards with this are the HDD fitted ones.

Re: Use SD card for memory

Posted: Thu Jul 18, 2019 5:22 am
by pik33
Or if the card is big enough, remove dphys-swapfile at all and create a real swap partition.

Re: Use SD card for memory

Posted: Thu Jul 18, 2019 5:38 am
by W. H. Heydt
Read up on "virtual memory". The inherent problem with all swapping systems is that all storage media are orders of magnitude slower than RAM (though Intel is *trying* to do something about that with Optane...but that's not something a Pi could use, anyway). And that's even before you start worrying about rapid wear on an SD card.

The best thing to do is to avoid needing to swap memory blocks out in the first place. This can be done by either having enough RAM to start with (hence the Pi4B2 and Pi4B4) or writing better code so that everything fits within the available RAM. The second best is to use good locality of reference to minimize swapping if it just has to be done.

The problems with wear on flash storage are *relatively* new, but the issues around swapping (and the worst case, thrashing) have been of concern directly since the 1970s, and indirectly since the 1950s (research "overlays"). This is *not* a new problem.

Re: Use SD card for memory

Posted: Thu Jul 18, 2019 5:52 am
by DougieLawson
pik33 wrote: ↑
Thu Jul 18, 2019 5:22 am
Or if the card is big enough, remove dphys-swapfile at all and create a real swap partition.
Why do you keep recommending this? It's no different. The swap file or swap partition is a file or filesystem in which to place a bucket of memory pages. How it's created doesn't affect how the kernel uses the bucket of pages.

Re: Use SD card for memory

Posted: Thu Jul 18, 2019 2:02 pm
by drllama
DougieLawson wrote: ↑
Thu Jul 18, 2019 5:52 am
pik33 wrote: ↑
Thu Jul 18, 2019 5:22 am
Or if the card is big enough, remove dphys-swapfile at all and create a real swap partition.
Why do you keep recommending this? It's no different. The swap file or swap partition is a file or filesystem in which to place a bucket of memory pages. How it's created doesn't affect how the kernel uses the bucket of pages.
There used to be a few issues with swapfiles vs swap partitions that resulted in slightly less performance for a swapfile:
  • Kernel handling of a swap file was slightly less efficient than a swap partition, this may or may not still be an issue.
  • No guarantee that a swap file used contiguous blocks/cylinders on disk. With flash this is absolutely no longer an issue.