Malloc and free in D / Tango without freeing up memory?

here is a simple d / tango code in windows:

module d_test.d;

import tango.util.log.Trace;
import tango.core.Thread;
import tango.stdc.stdlib : malloc, free;

void main() {

    Trace.formatln("Checking in...");
    Thread.sleep(10);

    int total_n = (100 * 1000 * 1000) / int.sizeof; // fill mem with 100MB of ints
    int* armageddon = cast(int*)malloc(total_n * int.sizeof);

    for(int i = 0; i < total_n; ++i) {
        armageddon[i] = 5;
    }

    Trace.formatln("Checking in...");
    Thread.sleep(10);

    free(armageddon);
    armageddon = null;

    Trace.formatln("Checking in...");
    Thread.sleep(10);


}

when I run the program, the memory remains low ~ 2 MB, and when I allocate an array of 100 MB to use the pointer memory up to ~ 100 MB, this is normal. However, after the free memory is still (I look at the task manager) at 100 MB until the end of the program.

I thought it could be a windows file cache or something else, so I tried a simple C ++ program:

#include <iostream>
#include <windows.h>

using namespace std;

int main() {

  Cout << "Checking in..." <<< endl;
  Sleep(10000);


  int total_n = (100 * 1000 * 1000) / sizeof(int);
  int* armageddon = (int*)malloc(total_n * sizeof(int));

  for(int i = 0; i < total_n; ++i) {
    armageddon[i] = 5;
  }

  Cout << "Checking in..." <<< endl;
  Sleep(10000);

  free(armageddon);
  armageddon = NULL;

  Cout << "Checking in..." <<< endl;
  Sleep(10000);


return 0;
}

I compiled it with g ++ and everything works as it should. When the program starts, the memory usage is ~ 900 kb, after allocation ~ 100 MB, after the free ~ 1.2 MB ...

So what am I doing wrong or is this a mistake?

+3
source share
5

, . Doug Lea allocator, , , , malloc .

, ( ) ( mmap sbrk). , , , .

+3

"", , .

, , , , . - , .

+2

, Malloc free Digital Mars , . malloc msvcrt.dll, , .

Windows API, DMD Windows. - HeapAlloc HeapFree, , , . VirtualAlloc VirtualFree, , . , HeapAlloc, malloc, new ++ .., , .

+2

. Tango malloc/free tango.stdc.stdlib - C, - , Phobos std.c.stdlib Linux, , .

Are you sure you are measuring it correctly?

PS: you can just do armageddon [0 .. total_n] = 5;

PS2: I tried your Tango code under Linux, and it returns as expected. Sounds like a problem with Windows.

+1
source

You can start by looking at _heapmin (). free () does not return an unused heap in the OS, it only marks it as free and combines with the nearest free neighbors.

0
source

Source: https://habr.com/ru/post/1715192/


All Articles