9.6. Optimizing Memory Usage
Often, it is common that a program that uses a large amount of memory can cause other performance problems to occur, such as cache misses, translation lookaside buffer (TLB) misses, and swapping.Figure 9-4 shows the flowchart of decisions that we will make to figure out how the system memory is being used.
Figure 9-4.
9.6.1. Is the Kernel Memory Usage Increasing?
To track down what is using the system's memory, you first have to determine whether the kernel itself is allocating memory. Run slabtop and see whether the total size of the kernel's memory is increasing. If it is increasing, jump to Section 9.6.2.If the kernel's memory usage is not increasing, it may be a particular process causing the increase. To track down which process is responsible for the increase in memory usage, go to Section 9.6.3.
9.6.2. What Type of Memory Is the Kernel Using?
If the kernel's Section 9.9.
9.6.3. Is a Particular Process's Resident Set Size Increasing?
Next, you can use top or ps to see whether a particular process's resident set size is increasing. It is easiest to add the rss field to the output of top and sort by memory usage. If a particular process is increasingly using more memory, we need to figure out what type of memory it is using. To figure out what type of memory the application is using, go to Section 9.6.6. If no particular process is using more memory, go to Section 9.6.4.
9.6.4. Is Shared Memory Usage Increasing?
Use ipcs to determine whether the amount of shared memory being used is increasing. If it is, go to Section 9.9.
9.6.5. Which Processes Are Using the Shared Memory?
Use ipcs to determine Section 9.9.
9.6.6. What Type of Memory Is the Process Using?
The easiest way to see what types of memory the process is using is to look at its status in the /proc file system. This file, cat /proc/<pid>/status, gives a breakdown of the processs memory usage.If the process has a large and increasing VmStk, this means that the processs stack size is increasing. To analyze why, go to Section 9.6.7.If the process has a large VmExe, that means that the executable size is big. To figure out which functions in the executable contribute to this size, go to Section 9.6.8. If the process has a large VmLib, that means that the process is using either a large number of shared libraries or a few large-sized shared libraries. To figure out which libraries contribute to this size, go to Section 9.6.9. If the process has a large and increasing VmData, this means that the processs data area, or heap, is increasing. To analyze why, go to Section 9.6.10.
9.6.7. What Functions Are Using All of the Stack?
To figure out Section 9.9.
9.6.8. What Functions Have the Biggest Text Size?
If the executable has a sizable amount of memory being used, it may be useful to determine which functions are taking up the greatest amount of space and prune unnecessary functionality. For an executable or library compiled with symbols, it is possible to ask nm to show the size of all the symbols and sort them with the following command:
With the knowledge of the size of each function, it may be possible to reduce their size or remove unnecessary code from the Section 9.9.
nm -S size-sort
9.6.9. How Big Are the Libraries That the Process Uses?
The easiest way Section 9.9.
9.6.10. What Functions Are Allocating Heap Memory?
If your application is Section 9.9.