3

This C code (compiled as C++) is not freeing memory. The program starts out with 992kB on the 'new' line, then after allocating memory, it goes to 10MB. After freeing the memory, it only goes down to 3MB. Even doing a delete[] doesn't erase the memory. What am I doing wrong?

INT iSize=8192;

struct sUsernameA
{
TCHAR *sUsername;
};

sUsernameA *sArr = new sUsernameA[iSize]();

for (INT i=0;i<iSize;i++)
{
sArr[i].sUsername = (TCHAR*)calloc(512,sizeof(TCHAR));
}


for (INT i=0;i<iSize;i++)
{
free(sArr[i].sUsername);sArr[i].sUsername = NULL;
}

delete [] sArr;
JeffR
  • 765
  • 2
  • 8
  • 23
  • 4
    How are you measuring the memory spent? – Baltasarq Jun 19 '15 at 18:44
  • 9
    This is a frequently asked question on StackOverflow - most malloc implementations maintain a pool of memory for new allocations, so you won't see everything freed up until the application exits. – Paul R Jun 19 '15 at 18:45
  • See also: http://stackoverflow.com/questions/30860188/does-free-free-the-memory-immediately – Paul R Jun 19 '15 at 18:52
  • @Baltasarq - just using Task Manager – JeffR Jun 19 '15 at 18:56
  • @PaulR - But this clears the memory immediately after calling free(): TCHAR *sTemp = (TCHAR*)calloc(10000000,sizeof(TCHAR)); free(sTemp);sTemp=NULL; – JeffR Jun 19 '15 at 19:00
  • 1
    The problem here is that the runtime decides based on some arbitrary method whether to release a large allocation at once, or leave it sitting around, based on some heuristics - for example how many and how large allocations the program has made. If you have an application that makes a lot of small allocations, then the runtime/OS will keep some memory around in case you do that again. If you make one large allocation, it's (perhaps) deciding to free it back to the OS at once. – Mats Petersson Jun 19 '15 at 19:04
  • 1
    That's just ONE possible scenario. It is VERY hard to prove that an application has or hasn't released it's memory, unless you use intrusive methods (such as asking the heap manager how much memory it has, and how much of it is actually free). – Mats Petersson Jun 19 '15 at 19:06
  • Well, it should be clearing it up, because after a few days of calling this program hundreds of times, my program shows it's using GB of RAM! – JeffR Jun 19 '15 at 20:00
  • 1
    That's probably because your program leaks. – David Heffernan Jun 19 '15 at 20:05
  • I meant to say that calling this particular code, not calling this program, it doesn't release the memory. – JeffR Jun 19 '15 at 20:14

1 Answers1

0

There are various approaches to memory management. For example, you can find an explanation to some of them in the wikipedia. I'm used to see dlmalloc around, but obviously there are many other techniques.

In any case, all techniques needs scafdolding, and also some of them try to guess the next moves about memory management by the programmer, so the only way to be sure that you've completely returned all the memory to the OS is to exit your program.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57