In this section:
Introduction
Inuse is a graphical tool designed to help developers avoid memory problems by displaying and animating in real time the memory allocations performed by an application.
By watching your program allocate and free dynamic memory blocks, you gain a better understanding of the memory usage patterns of your algorithms and also an idea of how to optimize their behavior.
Inuse allows you to:
- Look for memory leaks.
- See how much memory your application uses in response to particular user events.
- Check on the overall memory usage of your application to see if it matches your expectations.
- Look for memory fragmentation to see if different allocation strategies might improve performance.
You can run Inuse independently from Insure++; no relinking necessary.
Running the Inuse User Interface (Unix)
The Inuse Graphical User Interface (GUI) does nothing but wait for programs to start and connect to it, at which point it can display their memory activity. When these programs terminate, the Inuse window remains active until you choose to exit it. This allows you to analyze the data gathered during a program’s run once the program has completed execution.
If you exit Inuse while a program is still running, that program will continue running as usual but will stop sending memory activity data. You will have to start the inuse process again before memory activity can be displayed.
To run the example shown in this section, execute the following commands:
cp /usr/local/insure/examples/c/slowleak* .inuse
The first of these commands copies a set of example files to your local working directory, while the second starts Inuse. Normally, you will only have to execute the inuse command once. The Inuse process will remain running in the background, accepting display requests from any application that you choose to run.
Compiling and Linking For Inuse (Unix)
To use Inuse, you need to link your executable program with the insure
command. Compile the objects and libraries that make up your application with your regular compiler and link them with Insure++ to create a new executable.
Compile and link the sample program with the insure
command:
cc -c slowleak.c insure cc -o slowleak slowleak.o
If you are using a compiler other than cc
, you can tell Insure++ to use the correct compiler during the link step by adding a line such as insure++.compiler gcc
to a .psrc file.
The first command compiles the source file into an object module, while the second links with the special Inuse dynamic memory library.
Inuse is already available to your program if you are compiling your program with Insure++ to debug your code. With earlier versions, if you compiled with Insure++, Inuse would also display information about the Insure++ runtime as it performs error detection. This is no longer the case, because Insure++ now uses two separate heaps for the program. Nevertheless, we still recommend the method described above.
Enabling Runtime Activity Display (Unix)
Now that your program is linked with the appropriate libraries, you will need to enable the runtime memory activity display. To do this, you must add the following option to your .psrc file:
insure++.inuse on
Running the Application (Unix)
Once you have enabled the runtime display, you can run your application just as you would normally. Type the slowleak
command to run the slowleak example application.
Inuse Display (Unix)
When you start the example application, it will connect to Inuse. The Inuse display shows which applications are currently linked to the GUI. From this screen you can open any of Inuse’s visual reports. For a complete description of the Inuse display, see Inuse GUI.
Bug in the Slowleak Program (Unix)
Clicking the Hist button in the Inuse display will open up the Heap History window. As the slowleak program continues to run, a window similar to the following image should soon appear.
The window clearly shows that the program is continuously allocating more and more memory–the classic symptom of a memory leak. This type of pattern in an Inuse report is important to watch for in your own applications because it commonly indicates that something is wrong.
To find the cause of the problem, you can either review the source code manually or compile the program and run it with Insure++ using the following commands:
insure cc -o slowleak slowleak.c slowleak
To use Inuse, you only need to link with the insure command. To find the memory leak, you need to compile and link with Insure++, as shown above. For a description of detecting memory leaks with Insure++, see Memory Leaks.
To see the difference in Inuse’s output for correct and incorrect programs, you can either fix the problem in the slowleak example or copy, compile, and link the corrected version, noleak, with the following commands:
cp /usr/local/insure/examples/c/noleak.c . cc -c noleak.c insure cc -o noleak noleak.o noleak
If you exited Inuse, you will need to start it again before running the last of the commands shown above. You also need to have the insure.inuse on option set in your .psrc file to enable the graphical display, as explained in Enabling Runtime Activity Display (Unix).