Task 15: Hoarding Deallocated Memory
Hoarding is not an often-used feature. Its primary use is to prevent problems in which memory is deallocated by one part of the program but another part is using this memory, not knowing it is deallocated. For two examples of how hoarding is used, see Hoarding”.
Before reading this task, you should be familiar with the following information:
Chapter 1, “Locating Memory Problems”
An overview of memory concepts and MemoryScape.
Task 1: “Getting Started”
How to start MemoryScape with an overview of the kinds of information you can ­obtain.
Task 3: “­Setting MemoryScape Options”
How to configure MemoryScape so that it performs the activities you want it to perform.
Task 4: “Controlling Program Execution”
How to start and stop program execution.
To enable memory hoarding, select Extreme from the Memory Debugging Options screen. (described in Task 3: “­Setting MemoryScape Options”.) If you need to tune how MemoryScape hoards memory, select Advanced Options. Figure 73 shows the portion of the options page where you set hoarding options.
 
Figure 73: Hoard deallocated memory Option
Hoarding is actually a very simple process. When your program deallocates memory, MemoryScape stashes the deallocation request away. It also records information on the size of the block that would have been deallocated. And then it ignores the deallocation request.
Ignoring the deallocation request means that other parts of the program can continue to access this valid memory instead of using data that may have incorrect values. Said differently, the program can keep executing with the correct information a while longer. This improves your chances of identifying the location of the problem.
MemoryScape doesn’t ignore your program’s deallocations request indefinitely. By default, MemoryScape hoards 32 requests. Also by default, it defers only the deallocation of up to 256 KB of memory. You can, of course, change either of these values by using MemoryScape options.
To hoard all deallocated memory, set the maximum KB and blocks to unlimited by entering 0 in the hoarding control fields. To prevent or delay your program from running out of memory when using this setting, use the advanced option to set MemoryScape to automatically release hoarded memory when available memory gets low.
You can also set a threshold for the hoard size so MemoryScape can warn you when available memory is getting low. If the hoard size drops below the threshold, MemoryScape halts execution and notifies you. You can then view a Heap Status or Leak report to see where your memory is being allocated.