Android has decent memory management. It does not keep inactive processes and memory blocks (pages) in the main memory. Instead, Android writes the process status in the second memory. Hence, when you open an application again, it seems the app has never been closed because Android loads the app’s last state again. However, having an extra memory at hand is always useful. In this article, we cover how to make a swap for your Android device.
When an Android device faces with memory shortage, the operating system starts killing inactive processes and does the same task as explained already. Therefore, one could argue that using swap does not make sense since Android has a restricted memory and process management that does not allow memory to go to waste by inactive blocked processes. That is not always true. One could still benefit from activating a swap on an Android device. However, the performance boost depends on the device and the apps one uses.
There are two approaches to create and activate a swap on any Android device:
- Creating a swap partition
- Creating a swap file
For both approaches, your device has to be rooted.
Swap partition
To create a swap partition, you need to re-partition your SD card, if your device supports that. To accomplish that, you have to plug the SD card to a computer and use a partitioning tool such as GParted to create a swap partition. Finally, you can activate the swap partition using the swapon
command.
Since this approach can get unnecessarily complex and frustrating, we recommend against doing it. Instead, you can get the same result with the second approach, creating a swap file.
Swap file
The second way by contrast of the swap partition is quite clean and straightforward. You do not need to re-partition your memory card to make a swap. In this way, you make a swap file instead. All steps are done by the command line (Terminal emulator) of your device. Therefore, you don’t need to take out the SD card. Additionally, it works well, if your device doesn’t have a SD card slot.
You need to install a Terminal emulator application on your device. You can install the Qute terminal emulator.
The next application you need is Busybox. It is used to activate or deactivate the swap file.
After installing those apps, open the terminal emulator and cd
to your SD card as follows:
$ cd /sdcard
Then use the dd command to create the swap file. The size of the file is up to you. In the example below, the swap file capacity is 64 MB.
$ dd if=/dev/zero of=swapfile.img bs=1024 count=65536
Let’s break down the command parameter by parameter. We instructed the dd
command to create a swap file with the swapfile.img
name and fill its content with zero. Each block size is set to 1024 bytes or 1 KB. Lastly, we specified the file size in KiloBytes.
The next step is to format the file as a swap. For that, we need to use the mkswap command that is included in the BusyBox utility. For example,
$ mkswap swapfile.img
In order to activate the swap file, we use the swapon
command as follows,
$ swapon swapfile.img
Keep in mind that the above command has to be executed as root.
To check whether the swap file is activated or not, we need to execute the free
command. The output should show the capacity of the swap file, available swap space, and the occupied space.
Lastly, to turn off the swap, we can use the swapoff
command as follows:
$ swapoff swapfile.img
Again to validate whether the swap is off, you can runt the free
command.
Note: be careful when using the dd command. Inappropriate use of it can cause the erasure of your SD card data. It is not recommended to fiddle with any parameter of the given example, except the size. Additionally, before running the dd
command, make sure to backup your data on the SD card.
Conclusion
In this article, we firstly cover how Android memory management works. Then demonstrated two common approaches on enabling swap on any Android devices, namely by creating a swap partition or a swap file.