linux: app memory usage
May 10, 2009 5:39 pmOnce upon a time, I wrote a Linux app in C++ that used the database access libs from a well-known corporate database vendor. The app was very important to the employer I wrote it for. It had to run 24 hours a day, seven days a week. Unfortunately, the database access libs leaked like a screen door on a submarine. I did not have the source code for them and the vendor was not responsive to my pleas.
So I set up the app so it could restart itself every few hours, in order to clear its leaks. The app ran inside a script, and when it exited with a certain return value, the script would know to restart it immediately. This worked pretty well for a couple of months. Then I discovered that certain usage patterns would cause the app to completely exhaust its heap in as little as 30 minutes. It crashed, taking a lot of important data down with it.
My next try was a tad more sophisticated. I figured out how to determine how much memory the app was using currently, and it would restart itself when its heap size had grown to about 1.5GB. Now it didn’t matter how long it took for the program to exhaust its heap, it would always restart itself long before that happened.
I had planned to show you source code for getting a Linux app’s resident set size. Seems like a good complement to my earlier blog entry which had code to do the same thing for Apple’s platforms. But it requires a lot of code, due to boring details like file access, string manipulation, and so on. So I’ll just describe the algorithm, and let you implement it yourself, if you’re interested.
Getting a program’s resident set size on Linux involves querying the /proc pseudo-filesystem. First you must create a pretend “filename” that looks like this:
/proc/(pid)/statm
where (pid) is the pid of your program, which you can get with the posix function getpid(), converted to a textual representation. Open the “file” with whatever file manipulation APIs you normally use. fopen() is a good choice. Read the contents of the pseudo-file with fread(), then close it with fclose(). You should end up with a line of text that looks like this:
38546 48861 1677 104 0 36313 0
This is a list of various memory statistics about your program. The only one we’re interested in is the second number, 48861 in this example. This is your app’s resident set size, in pages. I’d prefer to have that value in bytes, so you can multiply the number of pages by the memory page size, which you can retrieve by calling getpagesize(). That’s it, now you’ve got a more-or-less accurate count of the number of bytes your Linux program is currently using.
Categories: linux, programming
Comments Off


No Responses to “linux: app memory usage”