BravoBug Tip: Fix leaky apps in OS X

Okay well maybe ‘fix’ is the wrong word, since we can’t really ‘fix’ the software that we use. Yet many users are unaware of what it means when a program ‘leaks’ memory, or what effects it can have on your Mac, and what to do about it. If that’s the case, here’s some quick tips on dealing with the problem:

What is a ‘memory leak’?

Programs ‘leak memory‘ when they ask the system for RAM, use it for something, and then forget to give it back. That’s the essential problem. It’s caused by bugs in the code, and it happens frequently, in many common apps. In reality, this is a bad thing for a program to do, but modern software can be so complex that bugs like this are sometimes allowed into final releases, or not noticed at all. Sometimes they can be very tricky for the programmers to find and fix. The Finder in OS X 10.5 leaks, for instance, when using the QuickLook feature on a series of MP3’s or sound files. I’ve also found Photoshop CS3 leaking from time to time. So really, it’s actually rather common.

How to spot it

The first thing you might notice is that your Mac is acting more sluggish than normal. Sometimes this is the result of the system having trouble providing enough RAM for all the programs that need it. OS X uses a virtual memory system where it pages in and out chunks of memory between RAM and your disk to allow programs to run and get the memory they need regardless, but ideally you want your programs to have access to real, available hardware RAM. When your system thinks it’s ‘out of RAM’, and it starts paging in and out, your Mac starts to slow down a bit.

Fire up Activity Monitor

There are several ways of monitoring memory usage (such as ‘top’ in Terminal), but Activity Monitor is easy and works quite well. Best of all, it’s already on your Mac. You can find Activity Monitor in your Utilities folder, which is located in your Mac’s Applications folder. Launch it, and you’ll see a window like the one below (click for full size).

To find apps that are potentially leaking memory, click the System Memory tab at the bottom of the window. You’ll see a nice, pretty pie chart showing your system’s RAM usage. Above it in the list box you’ll see a list of processes and programs running on your Mac. This list will be changing from moment to moment depending on what your Mac is doing, and what applications you’re running.

What you want to do is find out which of these apps are gobbling up the most memory. To do that, just sort the list of processes by clicking the RSIZE column header.

Take a look at the example screenshot above. You can see that on my Mac, right now, the OS X Finder is using about 37MB of real RAM. This is fine. But take a look at the Finder process a few minutes later after browsing through some MP3’s in the Finder, with QuickLook:

You can see that now the OS X Finder’s memory usage has ballooned in size! Now, just because a program (like the Finder) is using more memory does not mean it’s leaking. What often signals a leak is when a program or process continues to use more and more memory, without ever going back to it’s smaller start-up or idle size. In this case, I know the Finder doesn’t plan on returning this memory, because I’ve watched its memory usage, and it simply hangs onto it.

How to fix it

Thankfully, fixing apps that leak memory is very simple. Just relaunch them. Even though the program has forgotten to give up its memory that it borrowed, the system knows which memory was used by which applications. When you quit and re-launch an app, it’s like starting fresh again, from a clean slate.

The Finder is an application just like Photoshop, Preview, or TextEdit. Although the method of relaunching the Finder is a little less obvious than most other apps. To relaunch your Finder (if you need to), just hold the Option key and control-click, right-click, or click-and-hold on its Dock icon. You’ll see a nice handy Relaunch menu item. After relaunching, any memory that was incorrectly retained by the program should be freed up.

Final thoughts

As I mentioned above, it shouldn’t be assumed that just because a program is using a lot of memory, it is leaking. But it can be very eye-opening to watch your processes in Activity Monitor sometimes, and watch their memory usage as you use them over time. And if you’re not doing anything special in a program, and it’s still using a ton of memory, try quitting it or relaunching and seeing if you can free up some RAM for your other programs! The result will be a faster, happier Mac.

Comments are closed.