Debugging Memory Problems with MemoryScape™ : Chapter 1 Locating Memory Problems : Batch Scripting and Using the CLI : Using the dheap Command : Memory Reuse: dheap -hoard
Memory Reuse: dheap -hoard
In some cases, you may not want your system’s heap manager to immediately reuse memory. You would do this, for example, when you are trying to find problems that occur when more than one process or thread is allocating the same memory block. Hoarding allows you to temporarily delay the block’s release to the heap manager. When the hoard has reached its capacity in either size or number of blocks, MemoryScape releases previously hoarded blocks back to your program’s heap manager.
The order in which MemoryScape releases blocks is the order in which it hoards them. That is, the first blocks hoarded are the first blocks released—this is a first-in, first-out (FIFO) queue.
Hoarding is a two-step process, as follows:
1. Use the dheap -enable command to tell MemoryScape to track heap allocations.
2. Use the dheap -hoard -set on command to tell MemoryScape not to release deallocated blocks back to the heap manager. (The dheap ‑hoard -set off command tells MemoryScape to no longer hoard memory.) After you turn hoarding on, use the dheap -hoard
-set_all_deallocs
on command to tell MemoryScape to start hoarding blocks.
At any time, you can obtain the hoard’s status by typing the dheap -hoard command. For example:
d1.<> dheap -hoard
All Max Max
process: Enabled deallocs size blocks Size Blocks
1 (10883): yes yes 16 (kb) 32 15 (kb) 9
The Enabled column contains either yes or no, which indicates whether hoarding is enabled. The All deallocs column indicates if hoarding is occuring. The next columns show the maximum size in kilobytes and number of blocks to which the hoard can grow. The last two columns show the current size of the hoard, again, in kilobytes and the number of blocks.
As your program executes, MemoryScape adds the deallocated region to a FIFO buffer. Depending on your program’s use of the heap, the hoard could become quite large. You can control the hoard’s size by setting the maximum amount of memory in kilobytes that MemoryScape can hoard and the maximum number of hoarded blocks.
dheap -hoard -set_max_kb num_kb
Sets the maximum size in kilobytes to which the hoard is allowed to grow. The default value on many operating systems is 32KB.
dheap -hoard -set_max_blocks num_blocks
Sets the maximum number of blocks that the hoard can contain.
You can tell which blocks are in the hoard by typing the dheap -hoard -display command. For example:
d1.<> dheap -hoard -display
process 1 (10883):
0x804cdb0 -- 0x804d3b0 0x600 [ 1536]
flags: 0x32 (hoarded)
0x804d3b8 -- 0x804dab8 0x700 [ 1792]
flags: 0x32 (hoarded)
0x804dac0 -- 0x804e2c0 0x800 [ 2048]
flags: 0x32 (hoarded)
0x804fce8 -- 0x804fee8 0x200 [ 512]
flags: 0x32 (hoarded)
0x804fef0 -- 0x80502f0 0x400 [ 1024]
flags: 0x32 (hoarded)
You can enable autoshrinking when hoarding by entering dheap -hoard ‑autoshrink -set on. This allows the hoard to contract automatically when memory is short. When an allocation request fails because of a shortage of memory and autoshrinking is enabled, the HIA will eject a block from the hoard and automatically retry the request. This will continue until either the allocation succeeds, or the hoard is completely exhausted. In the latter case, the normal 'allocation operation returned null' event is raised.
One other feature is associated with autoshrinking: a notification threshold size, given in kb. If, during the course of autoshrinking, the size of the hoard in kb crosses from above to below the threshold size defined by
-autoshrink -set_threshold_kb num_kb, a new event is raised. This is to alert the user that space is running out. The event will be particularly useful when the size of the hoard is unlimited (which means that blocks released by the application are not returned to the heap manager), and therefore the size of the hoard really does reflect how much space is left.
One concern regarding the threshold event is that it could be reported many times if the size of the hoard fluctuates, crossing and recrossing the threshold. To reduce the noise, a count is associated with the threshold. The count is decremented each time the event is raised. This continues until the count reaches zero, after which the count is no longer decremented, and the event is not raised. Notification can be reactivated by re-priming the counter using the -set_threshold_trigger option. The default value for the number of times the event is triggered is 1. The trigger is independent of any event filtering that may be active.
The -autoshrink -reset, -autoshrink -reset_threshold_kb, and ‑autoshrink -reset_threshold_trigger options unset the TotalView settings for these controls. The HIA will determine its settings using the values in either the TVHEAP_ARGS environment variable, the HIA configuration file, or its default values.