Re: Memory leaks / Help with memprof & ps output



Jason Tackaberry writes:
 > 
 > Okay, but that's seems inconsistent with this simple malloc test:

It looks like malloc/free have gotten a lot smarter about giving back
space over the last few years!  I made a few tests based on your
program; it looks like they're able to figure out when big chunks have
been deallocated and give them back.  Here's a test that allocates a
large number of small objects (whose size isn't a convenient power of
2), and then deallocates all but the last one.  This test doesn't give
the space back until the program ends.

#include <stdio.h>
#include <glib.h>
    
int main() {
	char *p[1000000];
	int i;
	
	for (i = 0; i < 1000000; i++)
		p[i] = g_new0(char, 100);
	g_print("1. Check memory usage...");
	getchar();
	for (i = 0; i <  999999; i++)
		g_free(p[i]);
	g_print("2. Check again...");
	getchar();
}

But if I free that last one, it does!
-- 
Joseph J. Pfeiffer, Jr., Ph.D.       Phone -- (505) 646-1605
Department of Computer Science       FAX   -- (505) 646-1002
New Mexico State University          http://www.cs.nmsu.edu/~pfeiffer
Southwestern NM Regional Science and Engr Fair:  http://www.nmsu.edu/~scifair



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]