Memory-leaking programs

A memory leak is a program error that consists of repeatedly allocating memory, using it, and then neglecting to free it.

A memory leak in a long-running program, such as an interactive application, is a serious problem, because it can result in memory fragmentation and the accumulation of large numbers of mostly garbage-filled pages in real memory and page space. Systems have been known to run out of page space because of a memory leak in a single program.

A memory leak can be detected with the svmon command, by looking for processes whose working segment continually grows. A leak in a kernel segment can be caused by an mbuf leak or by a device driver, kernel extension, or even the kernel. To determine if a segment is growing, use the svmon command with the -i option to look at a process or a group of processes and see if any segment continues to grow.

Identifying the offending subroutine or line of code is more difficult, especially in AIXwindows applications, which generate large numbers of malloc() and free() calls. C++ provides a HeapView Debugger for analyzing/tuning memory usage and leaks. Some third-party programs exist for analyzing memory leaks, but they require access to the program source code.

Some uses of the realloc() subroutine, while not actually programming errors, can have the same effect as a memory leak. If a program frequently uses the realloc() subroutine to increase the size of a data area, the working segment of the process can become increasingly fragmented if the storage released by the realloc() subroutine cannot be reused for anything else.

Use the disclaim() system call and free() call to release memory that is no longer required. The disclaim() system call must be called before the free() call. It wastes CPU time to free memory after the last malloc() call, if the program will finish soon. When the program terminates, its working segment is destroyed and the real memory page frames that contained working segment data are added to the free list. The following example is a memory-leaking program where the Inuse, Pgspace, and Address Range values of the private working segment are continually growing:
# svmon -P 13548 -i 1 3
  Pid                         Command        Inuse        Pin      Pgsp  Virtual 64-bit Mthrd LPage
13548                          pacman         8535        2178      847     8533      N     N     N

Vsid     Esid Type  Description          LPage  Inuse    Pin  Pgsp  Virtual
    0       0 work  kernel seg               -   4375   2176   847     4375
48412       2 work  process private          -   2357      2     0     2357
6c01b       d work  shared library text      -   1790      0     0     1790
4c413       f work  shared library data      -     11      0     0       11
3040c       1 pers  code,/dev/prodlv:4097    -      2      0     -        -
ginger :svmon -P 13548 -i 1 3

  Pid                         Command        Inuse        Pin      Pgsp  Virtual 64-bit Mthrd LPage
13548                          pacman         8589       2178       847     8587      N     N     N

Vsid     Esid Type  Description          LPage  Inuse    Pin  Pgsp  Virtual
   0        0 work  kernel seg               -   4375   2176   847     4375
48412       2 work  process private          -   2411      2     0     2411
6c01b       d work  shared library text      -   1790      0     0     1790
4c413       f work  shared library data      -     11      0     0       11
3040c       1 pers  code,/dev/prodlv:4097    -      2      0     -        -

  Pid                         Command        Inuse        Pin      Pgsp  Virtual 64-bit Mthrd LPage
13548                          pacman         8599       2178       847     8597      N     N     N

 Vsid     Esid Type  Description          LPage  Inuse    Pin  Pgsp  Virtual
    0        0 work  kernel seg               -   4375   2176   847     4375
48412        2 work  process private          -   2421      2     0     2421
6c01b       d work  shared library text      -   1790      0     0     1790
4c413       f work  shared library data      -     11      0     0       11
3040c       1 pers  code,/dev/prodlv:4097    -      2      0     -        -