Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The number you are looking at unfortunately does not reflect what you think it does: the <strong>net</strong> amount of memory that your program has allocated using <code>new</code> calls that are unmatched by <code>delete</code> is probably less than what the operating system (OS) thinks your process owns (that's the number you are viewing here). </p> <p>This is because when you call <code>new</code> there is a layer called the heap manager between you and the OS. When you ask for a block of memory using <code>new</code>, the heap manager looks for an appropriate contiguous block of free bytes in its cache of OS memory, and only if there is none does it request another OS block. This results in the number you are watching increasing.</p> <p>When you call <code>delete</code>, the heap manager does not always release that memory back to the OS (that would be expensive, and cause excessive contention between processes). Most often, it just adds the released block back to an internal free list for later reuse elsewhere in your program. Over time, this in-and-out process from free list to in use and back can result in contiguous blocks of free bytes becoming hard to find. Every so often therefore, the heap manager will perform a compaction operation to coalesce free space into larger contiguous blocks, which can then be partitioned more efficiently when the need arises (to satisfy more <code>new</code> calls).</p> <p>To track your actual memory usage here, you could add global counters of <code>new</code> and <code>delete</code> calls and output at the end of your program whether they match up. </p> <p>A better way to do this would be to encapsulate the counter of 'active' objects as a static member of your <code>struct</code>, and increment it on construction and copy construction, and decrement it on destruction. That way you always can tell just how many active instances you have. If you have multiple threads in your program, such a counter would need to be maintained using ++ and -- in a threadsafe way - most OSes have atomic ways to do this eg. <code>InterlockedDecrement</code> and <code>InterlockedIncrement</code> on Windows.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload