Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help

Big Processes - Memory issues

When processes get big (dozens of Megabytes), specialised knowledge about memory usage can help you reduce the process's "memory footprint" - a good idea because

Preliminary investigation

The size of a compiled program on disk is not always a good indication of how big the resulting process will be when the program runs, and processes can grow as they run, so a useful first step is to monitor the process size.

On Windows try running the "Task Manager" (right-click on an empty part of the task bar). The administrator might have disabled it though.

On Unix machines you can use top, which every few seconds updates a screen of information like the following

last pid: 21455; load averages: 0.65, 0.76, 0.74 11:37:37 239 processes: 229 sleeping, 10 running CPU: 0.0% usr, 6.6% nice, 0.4% sys, 93.0% idle, 0.0% block, 0.0% intr Memory: Real: 187M/307M act/tot Virtual: 231M/397M act/tot Free: 444M TTY PID USERNAME PRI NICE SIZE RES STATE TIME CPU COMMAND ttyq8 15128 tpl 152 4 5871K 7661K run 4:07 0.06% mozilla-bin ...
It's the SIZE and RES columns that you need to look it. If the numbers keep increasing, there may be a "memory leak" in the program's loops. If the numbers are bigger than expected, it's worth trying to slim down your program. If you want something more graphical than top use gnomesystemmonitor on our Teaching System gnomesystemmonitor

Address space limits

If your processes are approaching 2GB in size, you might be in trouble. By definition, a 32-bit processor uses a 32-bit number to refer to the location of each byte of memory. 2^32 = 4.2 billion, which means a memory address that's 32 bits long can only refer to 4.2 billion unique locations (i.e. 4 GB). The 4GB limit is a theoretical limit - in practise programs won't be able to use that much on 32-bit machines. For example, with most versions of Windows 2GB is dedicated to kernel usage, and 2GB left for applications. Each application gets its own 2GB, but all applications have to share the same 2GB kernel space. You can change that by setting the 3GB switch in the boot.ini file so that 3GB is allocated to applications

A 64-bit processor can address more memory than you'll ever need.

Swap space limits

A 1G program doesn't need 1G of RAM (though it will help with speed!). It does however need 1G of "backing store" (otherwise known as "swap space") - an area on disc reserved for storing parts of processes that aren't being used. It's unlikely that Swap space limits will cause you problems - nowadays there's usually enough.

Kernel limits

The system manager sets various limits when configuring a system. On most unix systems
ulimit -a
will tell you what the limits are. Users might not have control over these limits. The information below refers to the teaching system's HP-UX machines, but other Unix systems are similar. Note that different limits may be exceeded depending on how variables are used and created. E.g.

Your code

You may not be able to increase the kernel limits, but you probably can reduce the memory requirements of your process. Sometimes this may require the CPU working harder, but if this means that your process will access the disc (backing store) less, your program is likely to run faster overall. Look at the big arrays first

Advanced diagnosis

Once you've tried dealing with arrays you may need guidance to decide what to do next. Profiling is used to deal with time-optimisation. Tools also exist to help with memory-optimisation. One example is Valgrind which works on x86-based linux systems. It includes
© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: July 2007