This post guides your through setting up a swap file for extending the RAM on the Arduino Yun. In the last blog post, Arduino Yun OpenWrt upgrade, sftp and nodejs , I installed nodejs on my Arduino Yun. Then when I tried to install some more node packages using node package manager (npm) i ran in to “out of memory” issue.
FATAL ERROR: Evacuation Allocation failed – process out of memory
At some point of time when you are running some process on the OpenWrt-Yun Linux side of the Arduino Yun, you are bound to face this error. Note that even when you are running a sketch on the Arduino side, if you happen to use any of the Bridge libraries, you are in essence running a process on the linux side. So inadvertently you might run a process that consumes more RAM than available on the linux side. For a reference here is the linux memory footprints on the Yun
|Type of Memory||Linux Microprocessor||Comments|
|Flash Memory||16 MB (9MB used by OpenWrt-Yun)||The linux flash memory can be expanded by using the SD Card to load more disk space as explained here.|
|RAM||64 MB||About 18 to 20 MB of this will be free for you to run other process.|
So when you run out of this 18+ MB free memory in RAM, we will run in to fatal error above.
To avoid this you should set up a swap file or a swap partition for the Linux side of the Yun. When you expand disk space as described here, the YunDiskSpaceExpander sketch doesn’t set up a swap partition by default. So we have to create a swap file that can be used by openwrt-yun to avoid running out of RAM memory. (Later when i have some time i will see if i can enhance the script to set up a swap partition by default)
Verifying Free Memory
Connect to Yun using ssh (i.e. by running “ssh email@example.com” from terminal). Then run:
This should show your current Free memory.. On mine its this:
total used free shared buffers
Mem: 61116 43556 17560 0 9612
Swap: 0 0 0
Note the Swap.. Its 0.
So now that i have confirmed that there is no swap file, I tried and set that swap file up on my Yun. The process involves 4 steps.
Step 1: Create an empty file to act as a swap file:
While connected to the Yun through the ssh terminal, run: (Note that this line will create a 512 MB swap file named yunswapfile in folder “/swap”and fill it with zero
dd if=/dev/zero of=/swap/yunswapfile bs=1M count=512
This should run for a bit and provide output like this:
512+0 records in
512+0 records out
Step 2: Designate the file as a Swap file:
The step above just created an empty file. To make sure it can be used as a swap file, run this from the shell:
You should get output like this:
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=e3e63fad-e6f7-4d4e-a32a-a326bbe48e8c
Step 3: Load the swap file for verifying
To verify that the swap file is good, try to load it by running this:
This will not provide any output if everything is cool. So verify by checking free memory.
total used free shared buffers
Mem: 61116 28644 32472 0 4888
Swap: 524284 0 524284
Viola!!! Now you can notice that a swap file is available for use by the RAM. Its not finished yet. Make sure you do step 4 below.
Step 4: Load the swap file as part of boot sequence
If you stop with Step 3, next time when you restart your Yun (linux part..either through power off/on or the Linux reset button near the LEDs) the swap file will not have been loaded. So to make sure that its gets loaded every time, you need to set the swap file as part of boot sequence.
Warning: The steps are fairly simple. But if you the steps are not executed fully you might leave a inconsistent boot config and Linux part of Yun may not load properly. Well this is Arduino. So you can reset the whole thing easily and try again. So please execute the following cleanly after understanding them.
//1. add swap config entry to fstab
root@youryun:/# uci add fstab swap
//2. set device config entry to swap. make sure you provide your full swap file name
root@youryun:/# uci set fstab.@swap.device=/swap/yunswapfile
//3. set swap is enabled
root@youryun:/# uci set fstab.@swap.enabled=1
//4. set file system type as “swap”
root@youryun:/# uci set fstab.@swap.fstype=swap
//5. set options to default
root@youryun:/# uci set fstab.@swap.options=default
//6. set fsck to 0
root@youryun:/# uci set fstab.@swap.enabled_fsck=0
//7. Commit the config changes. if you don’t run commit, the config changes will not be added. So make sure the changes are commited.
root@youryun:/# uci commit
That’s it. Done. Restart the Linux part of Yun (reset button near LEDs). After reboot, if you run “free -m” you should see the Swap file loaded. You have successfully expanded the RAM on your Arduino Yun’s linux side.