18

In windows it is possible to use usb flash drive as a virtual ram. I wonder if it is possible to do so in ubuntu? If it is, how could one do it?

[Edit]: I mean Ready boost equivallent for ubuntu. I know that it is not a perfect replacement for ram and it is not advisable for longterm use.

Is creating swap file in a usb flash drive similar to it? Will it demonstrate performance gains over a swap file in a Hard Disk Drive?

NlightNFotis
  • 2,500
  • 1
  • 16
  • 26
Tachyons
  • 17,281
  • 1
    If you mean to use it like Vista and Windows 7's Readyboost feature, you have to format it as a swap partition and assign a swap file to it. If you want to use it as extra system RAM, you can't. – Ringtail Mar 24 '12 at 16:34
  • yeah I mean readyboost – Tachyons Mar 24 '12 at 16:39

4 Answers4

13

The answer Sean suggests is one way of accomplishing this, another would be to just create a swapfile on the stick, that way you can also use your stick for other files. Like so:

  • find out where the stick is mounted. Look in nautilus or issue mount in the terminal

  • create an empty file of dd if=/dev/zero of=/media/YOURSTICK/swap bs=4096 count=131072. This creates a 512 MB file (512 * 1024^2 / 4096 = 131072)

  • create the swap and enable it sudo mkswap /media/YOURSTICK/swap && swapon -p 1000 /media/YOURSTICK/swap

The swap on your USB stick won't be faster than one residing on your HDD. Also it will kill the stick on the long term, writing does that to USB sticks.

To increase the usable amount of ram check out zram.

turbo
  • 4,622
  • I have already one swap partition ,i heard that more than one swap partition may lead to the problem,i wont use it in regularly ,but i want to use it when system have heavyload only. – Tachyons Mar 24 '12 at 13:32
  • 3
    If you have a workload where things are regularly being pulled in and out of swap to the extent where faster swap makes a significant difference, you're probably better off getting more RAM. Whether your swap is on a hard drive or USB drive, it is going to be orders of magnitude slower than real RAM. – James Henstridge Mar 24 '12 at 15:26
  • Yeah usb cant replace real ram ,But it may be better than real Ram(correct me if this is wrong :)) – Tachyons Mar 24 '12 at 17:51
  • 4
    USB is not better than real RAM – laurent Mar 25 '12 at 12:34
6

I found out about this when I did it from http://www.tuxgarage.com/2011/07/add-virtual-ram-to-ubuntu-using-spare.html

Plug-in your USB drive and make sure there are no important files on the drive. Delete all the files on the drive or re-format it and go to a Terminal.

First, we need to unmount the drive if Ubuntu auto-mounted it when you plugged it in:

sudo umount /media/drive-label

Where 'drive-label' is the name you see for the USB drive when it is mounted.

Or you can also do it this way:

sudo umount /dev/sdx1

Where 'sdx1' is your drive/partition letter. You can find it by running this command:

sudo fdisk -l

Now, we'll create the Swap file on your USB drive:

sudo mkswap /dev/sdx1

Where 'sdx1' is your drive letter we figured out from the output of 'fdisk -l' command above.

Now, turn on your new Swap:

sudo swapon -p 32767 /dev/sdx1

Done!

In order to make sure your new Swap is working, you can take a look at the output of this command:

cat /proc/swaps

My output is:

/dev/sda2 partition 2047992 60692 -1
/dev/sdb1 partition 7912708 17764 32767

To take the stick out first turn off the Swap:

sudo swapoff /dev/sdx1
dessert
  • 39,982
Sean
  • 79
5

If you truly need to add RAM to your system, there is nothing short of installing more memory sticks or increasing the physical memory of the machine.

Using a USB cannot increase the RAM in such a manner as to ONLY increase the swap file, plus the read/write times are decreased by the USB 480mb transfer limitations.

If you want to experience an increase in performance use "zRam". This adds a special file to your existing system and increases the system's virtual memory. It decreases the I/O write times as it uses a compressed block technology stored on the current RAM

More information here

Ringtail
  • 16,127
3

I already tried this ago. But usb stick so slow when writing something.

I experienced very short hang continuously when system using usb swap. It was may my stick was so cheaper one.

Mait
  • 356