Showing posts with label cheap fixes. Show all posts
Showing posts with label cheap fixes. Show all posts

Friday, May 11, 2012

Easing Memory Issues on the Cheap


At the end of this post there will be a brief tutorial on how to improve performance on computers that are strapped for cache for the cost that ranges from "pulled it out of a box of junk" to $5 at your favorite retailer (even the grocery store). It's no more or less than a step-by-step guide to taking your swap off of your ancient spinning platter drive and onto a USB flash drive. After another sentence or two of introducing the idea, I'll go into a spiel on why using Linux is "green." If you're not into that sort of thing, skip to the last section.

Most of the time, my aging laptop gets along just fine without having to swap anything off to the disk. Even if I'm making a panoramic image or editing or transcoding video, I can often keep my swap usage to under 20 MB. If I get much past that 20 MB mark, though, the thrashing becomes unpleasant.

The other day, I was rendering an HD video and I noticed my swap usage approaching 200MB when I remembered reading about ReadyBoost, which allows Windows to use USB mass storage devices for swapping. It's a great idea because USB flash drives have better read/write speeds than platter-based storage (alleviating or delaying thrashing) and because USB flash is a lot more economical than most solid state media. When your $5 drive wears out quickly, you won't be too bothered.

Of course, using USB flash as a swap partition (or to store a swap file) has been possible on Linux for as long as there has been support for USB mass storage on Linux. It's just that I've been reading about Linux every day for many years and I've never seen it discussed. Maybe I'm the only one who's been missing out. Somehow I doubt it.

Green computing, or using a computer until it dies

As several of you are already aware, I was a longtime Red Hat / Fedora user before I switched to Slackware earlier this year. I wasn't unhappy with anything about Fedora other than its performance -- even using 4 KB of swap meant I was in for a bad day.

I'd had previous good experiences with Slackware having relatively new versions of my favorite packages, great stability, and sane, readable configuration options. To top it off, I'd used it in the past to squeeze good performance from truly ancient hardware. Slackware used to be my go-to distro for older computers. Now that my only computer is older, I've gone to it.

When I finally get a newer machine, I'll stick with it. I'm impressed by how straightforward it is. Best of all, the work I put into untying the knot (getting it run initially) has allowed me to experience a new state of mind where things just make sense and good things come to those who do little-to-nothing.

I've been duly rewarded for the switch. As I mentioned, the threshold for thrashing has been raised from 4 KB to 20 MB, and Slackware has changed my video editing metric from "abject despair" to "frustrating." I don't expect I can do much better than "mildly frustrating" at video editing, since I have an integrated graphics chip.

So far, I've focused on how Slackware has kept me using different binary packaging of the same programs with better performance. That is, I'm talking about a Linux-to-Linux switch that's kept me happily using the same old gear for longer. What make it "green" is that my trusty old laptop isn't in the landfill yet. Now, I've never run Windows on this laptop, but I have a story that should put the inherent greenness of switching to any Linux distro in perspective.

My graphics chip is so bad that when it was still brand new, it was the subject of a class action lawsuit. Careful readers and pedantic fanbois will no doubt point out that it was an unsuccessful suit, but I argue that even in a litigation-happy country like the United States things have to get bad before people will sue over computer chippery. Anyhow, the chip as allegedly less than "Vista capable." Where Aero would have crippled me, Compiz was working alright and now with Gnome Shell things are going so smoothly that I forget that it demands hardware acceleration.

Wikipedia reports that the chip will, in fact, run Aero. I suspect that it only does so begrudgingly, and you can forget about its running Crysis. You can split hairs if you like, or you can accept my simple point that this laptop would barely get going with Windows, yet I use it for photo editing, audio/video editing, and all the daily browsing, music-playing, and video-watching you'd expect.

Of course, I might just upgrade the RAM if things are getting a little sluggish. As it is, I've already maxed out at 2.8GB, slightly more than the manufacturer-reported maximum of 2GB. Still, that's not enough for editing videos and other heavy tasks.

Now that I've begun using USB flash for my virtual memory needs, I've managed to squeeze out a bit more performance. That doesn't change the fact that I don't have a dedicated GPU, and my CPU has to carry the full weight of rendering the videos, but it does make it so that the I/O is no longer the bottleneck.

That is, the problem used to be swapping information onto my PATA hard drive (which isn't as impressive as it once was). Now that swapping isn't a problem, my overworked CPU becomes the weakest link, and modern multitasking allows me to keep browsing the web or listening to music while my video renders. In fact, if I wanted to (and I did, for testing purposes), I can open an instance of every GUI program I have without experiencing any slowdown like I did before.

You shouldn't expect a miracle. If you couldn't play HD videos before, you probably won't be able to now. But if financial worries will keep you using the same PC for a few more years, you should at least get a responsive desktop out of the deal.

The Tutorial

There are two possibilities for swapping on a USB flash drive. You can allocate an entire partition to the task (the whole drive, or a part of it with the rest used for storage), or you can make a swap file. Since I make frequent use of an external hard drive for storage and backup needs, I've chosen the latter option. To clarify, my external drive isn't a flash drive, but I want to be careful not to use it for swapping by accident.

If you're using the swap file option, first you make the file itself. Mount the drive you want to use, then issue the following command as root:

dd if=/dev/zero of=/media/yourusbdisk/swapfile bs=1024 count=512000

Where:
dd is a utility for copying and converting files
The if argument specifies the input file or device. In this case, we're just reading zeros.
The of argument specifies the output file or device. In this case, we're writing those zeros to a file called "swapfile" on the disk called "yourusbdisk."
The bs argument specifies how many bytes we want to read and write at a time. A bs of 1024 means we're working with 1KB of information per count.
The count argument specifies how many times to copy information from /dev/zero to your swapfile. In other words, this is how big the file will be. You can figure out what your count argument should be by multiplying the desired file size in MB by 1024. So, a count of 512000 yields a file that's 510 MB or 512MiB.

Whether you're using a swap file or a swap partition, you'll need to use the next steps:

For files: mkswap /media/yourusbdisk/swapfile
(Where that argument should be the location of the file we made in the previous step)

For partitions: mkswap /dev/sda1
(Where /dev/sda1 is the partition you want to make your swap partition. Hint: it probably won't be sda1.)

Now, all we have to do is turn off our slow platter-based swap partition:
swapoff /dev/cryptvg/swap
(Where the argument is your swap partition.)

And turn on our flash-based swap partition:
swapon /media/yourusbdisk/swapfile

If you've got enough new swap space and very little RAM, you might want to increase your swappiness. For instance, if you only have 512 MB of RAM, but you've got 1GB or more of flash-based swap, you'll want to turn it up to 100:
echo 100 > /proc/sys/vm/swappiness
 
After you reboot, your swap file/partition will still exist, but it shouldn't be automatically kept on to the next
session unless you specifically make it so. See your distribution-specific tools for doing this. Likewise, you'll
need to take further steps to permanently alter your swappiness parameter.

You can add me on Google+ to read my latest writings as soon as I post them. You can also call me bad names or write to thank me in the comments section.