www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Memory Leak with C functions?

reply "Namespace" <rswhite4 googlemail.com> writes:
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?
Nov 28 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Nov 28 2013