Evaluating Memory and Cache Usage

Previous Topic Next Topic

Investigating User-Mode Memory Leaks

A memory leak occurs when applications allocate memory for use but do not free allocated memory when finished. As a result, available memory is used up over time, often causing the system to stop functioning properly. Therefore, it is important to investigate the causes of all memory leaks, particularly on server computers. This section describes how to identify memory leaks, including ones that affect the critical nonpaged memory pool, and where to find tools and information related to working with memory leaks.


note-icon

Note

It is sometimes possible to mistake an increase in system load for a memory leak. To distinguish between these conditions, observe the Memory and Process counters over a number of days. If you see the system first reach a steady state, then attain a level of increased load (usually achieved during some peak portion of the day), and then fall again, it is likely you are seeing variations in load rather than a leak. On network computers, look at user sessions and throughput rates such as transferred bytes per second to eliminate workload as a factor.

Identifying a Memory Leak

The symptoms of a memory leak include the following:

Figure 6.7    Out of Virtual Memory Error Message
Enlarge figure

Figure 6.7 Out of Virtual Memory Error Message

If you suspect that a particular application or service is causing a memory leak, investigate the memory use of your applications using the following counters:

Monitor these counters over a period ranging from two hours to a few days. Logging is recommended, both because of the overhead of monitoring multiple instances of the Process counters and because leaks tend to manifest themselves slowly.

In addition, to isolate the problem and avoid unnecessary overhead, monitor from a remote computer, if possible. Network activity or interaction with other computers can interfere with the results.

Memory Leaks and the Nonpaged Pool

Although any leak is serious, memory leaks are of particular concern when they involve the nonpaged pool. Many system services allocate memory from the nonpaged pool because they need to reference it when processing an interrupt and cannot take a page fault at that time. To identify whether or not a leak affects the nonpaged pool, include the following counters in your monitoring:


note-icon

Note

Because the internal counters used by Task Manager, Process Monitor, and System Monitor to measure the size of the nonpaged pool for each process are not precise, it is recommended that you monitor changes in the overall pool size over time (a few days, for example), rather than rely on the absolute, instantaneous values reported for each process. The counter values are estimates that count duplicate object handles as well as space for the object. Also, because the process pool size counts are rounded to page size, pool space is overestimated when a process uses only part of a page. In contrast, total pool size counts are precise. Therefore, the sum of pool sizes for each process might not equal the value for the whole system.

The counters on the Memory object monitor the total size of the nonpaged pool and the number of allocations of pool space for the whole system. The counter on the Process object monitors nonpaged pool space allocated to each process.

To use System Monitor to monitor the nonpaged pool for leaks, follow these steps:

The following additional tools provide information about the paged and nonpaged memory pools as listed in Table 6.3. These tools collect their data from the same sources.

Table 6.3 Tools That Provide Information About Memory Pools

Tool Name Description Location
Memsnap (memsnap.exe)1 Records system memory usage to a log file. Windows 2000 Support Tools
Process Monitor (pmon.exe)1 Provides total and per process values for nonpaged and paged pool memory.
Also monitors the committed memory values shown in the Pmon display for increases; the process with the leak should have an increasing value reported under Commit Charge.
Windows 2000 Support Tools
1 These tools are useful because they show allocations on a per-process basis.
 

For information about installing and using the Windows 2000 Support Tools and Support Tools Help, see the file Sreadme.doc in the \Support\Tools folder of the Windows 2000 operating system CD.

For a quick demonstration of a memory leak, start LeakyApp, a test tool on the Windows 2000 Resource Kit companion CD, and observe the values of the monitored counters. Notice the steady increase in the following counters: Memory\Pages/sec, Process(LeakyApp)\Working Set, and Process(LeakyApp)\Private Bytes.

Figure 6.8 illustrates counter activity during a memory leak generated by the LeakyApp tool.

Figure 6.8    Process Memory Activity During a Memory Leak
Enlarge figure

Figure 6.8 Process Memory Activity During a Memory Leak

Although the memory leak illustrated in Figure 6.9 has a systemic effect, the problem can be tracked to a single cause—the leaking application. If you have an application that exhibits similar behavior, it is recommended that you either modify it (if you have access to the source code) or replace it with another program.

Developer tools for analyzing and tuning memory usage by applications are available on the Windows 2000 Resource Kit companion CD. For more information about developer tools, see the MSDN link on the Web Resources page at http://windows.microsoft.com/windows2000/reskit/webresources.

The following tools optimize memory-intensive applications.

© 1985-2000 Microsoft Corporation. All rights reserved.