November 28, 2013 Memory Leak with C functions? | ||||
|---|---|---|---|---|
| ||||
Code:
----
import std.stdio;
import core.stdc.stdlib : calloc, free;
void main() {
readln(); /// ~ 1332 K
void* ptr = .calloc(100_000, int.sizeof);
readln(); /// ~ 1720 K
free(ptr);
ptr = null;
readln(); /// Still ~ 1720 K
}
----
If I compile this code with dmd test.d and execute it, I see a memory usage of ~ 1332K. If I press enter the programm allocate 100_000 int's and the memory usage increased to ~ 1720 K. If I now press enter I assumed that the memory usage decreased to ~ 1332 K again, but it is still ~ 1720 K. Why?
| ||||
November 28, 2013 Re: Memory Leak with C functions? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | Namespace:
> If I compile this code with dmd test.d and execute it, I see a memory usage of ~ 1332K. If I press enter the programm allocate 100_000 int's and the memory usage increased to ~ 1720 K. If I now press enter I assumed that the memory usage decreased to ~ 1332 K again, but it is still ~ 1720 K. Why?
This is system-dependent. Sometimes the memory allocated by a program is released to the OS only when the a program ends. But that memory is available to your C heap, so if you call calloc again after the second readln, the total memory used should change very little.
Bye,
bearophile
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply