How to make swap for your Android device

Android has a great ability in memory management. Android does not keep inactive processes and memory blocks (pages) in main memory, instead it writes the process status in second memory and when you open the application again it seems that the application has never been closed since it the last state will be loaded again. From another point when system will face with shortage of memory Android start killing inactive processes and does the same task as explained already. Therefore, using swap actually does not make sense since Android has very strong memory and process management which does not allow memory to be wasted by inactive blocks or processes but as a matter of curiosity, you can make and activate swap memory in Android, however, you may not realize any performance improvement in your device.
For making swap and activate it, two ways are available and for both your device has to be rooted.
The first way is to make swap partition by repartitioning your memory card (SD card). In fact you need to accomplish some steps in your computer like partitioning. Additionally, you need to install certain application(s) as well. To summarize this way is quite messy and somehow time consuming which I do not prefer it.
The second way by contrast is quite clean and neat way. You do not need to repartition your memory card to make swap file. In this way you make swap file instead of swap partition and all steps are done by command line (Terminal emulator) of your device. So, you need to install Terminal emulator application on your device. The next application which you need it, is Busybox to activate or deactivate swap file.
After opening terminal application with use of “cd” command go to your SD card partition like below,

$ cd /sdcard

Then with “dd” command you need to create a file which is known here as swap file. The size of file is up to you. In the example, swap file capacity is 64 MB.

$ dd if=/dev/zero of=swapfile.img bs=1024 count=65536

The above command make a file and named it “swapfile.img” then full the file with zero content and size of each block of the file is 1024 byte or 1KB. In the last parameter size of the file has been set in KB.
Now you need to format file or more preciesly making structure for the file that could be usable. Therefore, you need to do it with “mkswap” of BusyBox utility application. Take look at the example

$ mkswap swapfile.img

Mentioned command just restructure the file which can be usable as a swap file.
Now for turning the swap on you need to type the following command.

# swapon swapfile.img

Keep in your mind that above command has to be run by root user.
In order to test that is it your swap file activated or not use “free” command. In the output of the command you should see the capacity of the swap file, available swap space and occupied swap space.
For turning the swap off see the following example,

$ swapoff swapfile.img

Note: be careful in the use of “dd” command since inappropriate use of it can cause to remove all of your available data on your memory card. It is highly recommended to not change any parameter of “dd” except size. Before, applying any changes sure that you fully read and understand the manual page of it.

send your idea and information to kasra.madadipouya@geeksweb.eu.org

Leave a Reply