Using MemTrack to Isolate Memory Leaks


After detecting a memory leak, you can use MemTrack to attempt to isolate the cause of the memory leak in a specified application.

MemTrack

MemTrack is a tool that can be used to track memory usage. Its primary use is to track down the source of a memory leak in an application. To enable Memtrack, you must set a couple of image flags and then remake your image in PB.

Memalyzer

Memalyzer is a tool that can be used to process the raw data in the log file generated by MemTrack. Memanalyzer will output the log using a more informative format.

When you run the Memalyzer tool on the log file generated by MemTrack (celog.clg), a text file is created that provides information that can be used in locating the source of a memory leak in a specified application.

The Memalyzer is a desktop tool, which is located at Public\common\oak\bin\i386.

Creating a MemTrack Build

To build a platform with MemTrack enabled in PB:

  1. Open the Build Options tab of the Platform Settings dialog box.
  2. Select Enable Event Tracking During Boot and Flush Events to Release Directory.
  3. Click “OK”.
  4. Choose Build Platform from the Build menu.
  5. Choose Download Image from the Target menu.
  6. Go to the release directory (i.e. C:\platform\cepc\reldir\X86_Debug) and confirm that the celog.clg file has been created.

Isolating a Memory Leak

You can use MemTrack by issuing commands from CESH. To turn on MemTrack for a MemTrack build, use the memtrack on command at the cesh prompt. The MemTrack command syntax is as follows:

memtrack on | off | baseline

You can run this command from CESH by choosing CE Target Control from the Target Menu in PB.

To isolate memory leaks using MemTrack:

  1. Start the application in which you would like to track down a memory leak.
  2. Turn on MemTrack.

    Choose CE Target Control from the Target menu in PB.

    At CESH, type:

    memtrack on

  3. Set your first baseline. At CESH, type:

    memtrack baseline

  4. Replicate the behavior that you suspect is causing the memory leak.
  5. Set another baseline. At CESH, type:

    memtrack baseline

  6. Repeat step 4 and 5 until you have enough repetitions to create and detect a memory leak.
  7. Stop MemTrack. At CESH, type:

    memtrack off

  8. In the command line build window, navigate to your release directory.
  9. Use the desktop tool, memalyzer.exe, to parse the interesting allocations from celog.clg to a readable text file. At a DOS prompt, type:

    Memalyzer –name applicationname.exe celog.clg > applicationname.txt

  10. Open the text file (.txt) in Notepad.
  11. Choose Find… from the Edit Menu, and perform a search on “Memory unfreed chains summary”.

    In the text file (.txt), any allocations listed under this text string may imply a memory leak in your application.

NOTE   BSTRs are cached by the automation library (oleaut32.dll) and memory is not immediately freed on calling the SysFreeString function. This can cause invalid leak reports in the memanalyzer output when tracking BSTRs.

Sample Output

The following sample .txt output shows an allocation listed under this text string that was caused by an unfreed BSTR:

      ALLOC 0x0c15ed78 (432 bytes) Mark=3 TID=0xa3bb3fba
   0x01fb568d gwes.exe!coredll.dll!_HeapAlloc + 0x000000e6
   0x01aea0ab gwes.exe!ole32.dll!?CRetailMalloc_Alloc@@YAPAXPAUIMalloc@@K@Z + 0x0000003d
   0x01ae9f84 gwes.exe!ole32.dll!_CoTaskMemAlloc + 0x00000018
   0x01a96fdb gwes.exe!oleaut32.dll!?AllocCachedMem@APP_DATA@@QAEPAXK@Z + 0x00000095
   0x01a8a7b5 gwes.exe!oleaut32.dll!_SysAllocStringLen + 0x0000007d
   0x01a8a733 gwes.exe!oleaut32.dll!_SysAllocString + 0x00000023
   0x0072d718 gwes.exe!chfc.dll!?Parse@CASTextView@@AAEXPAG@Z + 0x000000d9
   0x0072e04e gwes.exe!chfc.dll!?put_Text@CASTextView@@UAAJPAG@Z + 0x00000028
   0x0071d7b5 gwes.exe!chfc.dll!?fmmbInitForm@CASActiveForms@@QAEJPAU_FMMBI@@@Z + 0x0000004f
   0x0071ba70 gwes.exe!chfc.dll fmmbDisplayMessageBox@CASActiveForms@@QAEJPAU_FMMBI@@@Z + 0x000000d5

Show: