Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How does it work getmem/malloc/free?</p> <p>Heap Allocator - As used by malloc...</p> <p>1) Internally allocates large chunks(typically 64K up to 1Megabyte) of memory and then sub-divides the chunks up to give you the 100byte and 200byte objects and strings in the program. When you free memory all that happens is the place where it was allocated from in the internal buffer or chunk is then marked as free. NOTHING ACTUALLY HAPPENS!</p> <p>2) So you can think of the HEAP as a list of big chunks of memory, and all the objects in your program are just little parts of those chunks.</p> <p>3) The big internal chunks of memory are only freed when all the objects inside them have been freed, so the usual case is that when you free some object that nothing actually happens except some bits get marked as being available.</p> <p>That is a fairly naive description of the heap system, but most heaps work in a similar way, but do a lot more optimization than that. But your question is why does the memory not go down and the answer is because nothing actually gets freed. The internal pages of memory are retained for the next call to "new" or "malloc" etc...</p> <p>PICTURE IT</p> <p>INSIDE HEAP IS ONE HUGE BLOCK OF 100Kb</p> <pre><code>You call "malloc(1000)" or "getmem(1000)" to get a 1K block of memory. </code></pre> <p>Then all that happens is that the 1K block of memory is taken from the 100kb block of memory leaving 99K of memory available in that block. If you keep calling malloc or getmem then it will just keep dividing the larger block up until it needs another larger block.</p> <p>Each little block of memory allocated with a call to malloc or getmem actually gets about 16 or 24 extra bytes(depending on allocator) extra memory. That memory is bits that the allocator uses to know what is allocated and also where it is allocated.</p>
 

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