Ubuntu – How to fix the lag when running Ubuntu installed on a USB drive

performanceusb-drive

I recently did a fresh install of Ubuntu 12.04. I did it on a 16GB USB flash drive, with 964MB for swap and 13.97GB for ext3.

System specs:

  • 2GB DDR2 RAM (800~ frequency range)
  • 3.14GHz single core processor
  • ATI 3600 graphics card

This is the first time I have gotten Ubuntu installed on a USB flash drive (I had another Ubuntu installed on hard drive). The Ubuntu I had on the hard drive worked marvellously faster than my Windows 7; for example, I could be running Google Chrome with around twenty tabs and have Photoshop running without any lag. But now with the USB version I can barely have Google Chrome open with six tabs and browse my home folder at the same time without lag, and freezing of either Chrome or the home folder.

This is a picture of top being used in the terminal:

So could you please help me? Any suggestions will be helpful.

Best Answer

Where USB flash drives fail is in random writes to the disk. They can be hundreds of times slower than the same operation on traditional hard drives. Note that this used to be the case with SSDs too until around 2008 or 2009 when they gained new controller technologies which make them much faster than tradtional hard drives. These technologies pretty much don't exist on USB flash drives.

Operations that will be particularly slow include:

  • Installing new packages, or doing system updates. Expect these to be tens of times slower than traditional hard drives, maybe even 1000s of times slower than SSDs. Upgrading between Ubuntu versions could take several hours or more.
  • General browser use, given that your browser caches files to disk all the time.
  • Operations which involve copying or moving/altering hundreds of small files.

This article on phoronix compares the performance of various file systems on USB flash drives. Unfortunately, its conclusion is that it doesn't make all that much difference. Theoretically, a log-structured file system should help, but these aren't mainstream enough for me to recommend them for the uninitiated.

One idea is to use a read-only or hybrid file-system instead, which is exactly what you get if you copy the Ubuntu Live CD image (or use UNetBootin to set up a live USB) to your USB flash drive. Look into setting up a Live USB from the Live CD image with persistence (try to find a recent guide to doing so). Of course, you then won't be able to upgrade to a new version of Ubuntu, but that's not necessarily terrible.

Barring any such measures, and keeping a traditional partition type/structure, your main strategy will be to try and minimise writes to the disk at all cost. Ideas for this include:

  • Disable journalling on ext4 or set your partitions to "writeback" mode. The drawback to this is that it increases the chances and severity of data loss or corruption in the case of accidental power loss, crash or simply unplugging the drive while it's being used.
  • Disable swap, though on a system with sufficient RAM this actually will have much less of an effect than you expect, and on a system with insufficient RAM may cause stability problems.
  • Try to minimise on-disk caching by various programs, such as your browser. Reducing the size of the disk cache is not necessarily enough: turning it off all together and using a memory cache (if possible) is much better. If you have lots of RAM, you could try using a tmpfs mount (like a disk partition in RAM) for various tmp and cache directories.