Memory usage does not decrease when using free ()

I am writing an OS independent unoccupied queue, so far it works great, but there is a small memory management issue. I'm not sure if its the problem with gcc or mine. Problem: memory grows when an element is added to the list, but when an element is removed from the list (free (elementPointer);) the memory usage doesn't change.

BUT when I use pthreads, N-producers and M-users (1<N<20, 1<M<20)

are using about ~ 10mb memory all the time (when trying to add and remove ~ 10kk items) free seems to work.

And the funny thing is that in VS 2010 (same code, no threads) it works fine for free, memory is released (observable task manager).

I did a test, added 1kk items, after adding all, removed all items one by one (no streams).

Linux - 0.08 seconds

Windows ~ 57 seconds

Linux (free) - 0.07 seconds

Windows (no freeware) - 0.9 seconds

So the question is, why isn't memory freed in Linux C when threads are not being used? I can post the code if needed.

GCC version: 4.4.3

+2


source to share


3 answers


In many operating systems, it free()

does not free memory for the OS, but "only" for new calls malloc()

. This is why you don't see the memory usage going down externally, but when you increase the number of new allocations by streaming, the memory is reused, so the overall usage doesn't go through the roof.



+7


source


Malloc should not return memory to the operating system. Most malloc implementations on unix-like systems do not. Especially for small objects.

This is done for performance reasons.



I just noticed that this is potentially unclear. By "malloc" I mean the entire subsystem associated with the malloc function - malloc, free, realloc, calloc, and any special functions your libc may implement.

+6


source


To make things easier, there are two memory managers in heap allocation: OS Memory Manager and Process Memory Manager (which can be more than one). The OS Memory manager allocates "large chunks" of memory to individual process memory managers. Each process memory manager keeps track of allocated segments as well as "free segments". The process memory manager does not return free'd segments to the OS Memory manager because it is more efficient to hold onto it if it needs to allocate more memory later.

+3


source







All Articles