tldr6501
Posts: 1
Joined: Wed Jul 17, 2019 3:41 pm

Use SD card for memory

Wed Jul 17, 2019 3:43 pm

I was just told you can use an SD card to increase the memory. Is this correct?

User avatar
neilgl
Posts: 952
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Use SD card for memory

Wed Jul 17, 2019 7:43 pm

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.

Andyroo
Posts: 4466
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Use SD card for memory

Wed Jul 17, 2019 8:05 pm

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.
Need Pi spray - these things are breeding in my house...

pik33
Posts: 182
Joined: Thu Sep 10, 2015 4:26 pm

Re: Use SD card for memory

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.

W. H. Heydt
Posts: 10883
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Use SD card for memory

Thu Jul 18, 2019 5:38 am

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.

User avatar
DougieLawson
Posts: 36105
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Use SD card for memory

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

drllama
Posts: 12
Joined: Thu Jun 27, 2019 2:05 am

Re: Use SD card for memory

Thu Jul 18, 2019 2:02 pm

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.

Return to “General discussion”