0

I have a code that new Bitmap, but when I use delete, the memory usage in Task Manager is not reduced.

My code is as below

Bitmap* pBuffer = new Bitmap(pMainImage->GetWidth(),  pMainImage->GetHeight());
delete pBuffer;

How to delete pBuffer above appropriately? So the memory usage return to the level before new Bitmap() is called.

roscoe_x
  • 609
  • 1
  • 9
  • 16
  • What Task Manager shows doesn't decrease after you free memory. It's up for new allocations. – chris Jan 23 '13 at 03:09

1 Answers1

2

That's just not how the Windows memory manager works. When you release memory, the heap blocks just get marked as "not in use". Available for the next allocation. It does coalesce free blocks and decommits the virtual address space but it is not in a hurry to do so. The exact rules it uses are not documented and vary by operating system version.

Task Manager is otherwise not up the job to be a reliable indicator of actual virtual memory in use. Most of all because that is not simple to do. The HeapWalk() api function can do it but comes with a strong warning that actually using it has detrimental side-effects. Caused by it having to take too many locks to make it safe. Only a debugger that freezes all threads could do it without those side-effects. Like Windbg's !heap command.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536