memory allocation/deallocation (malloc experts needed)

Dan Nelson dnelson at allantgroup.com
Wed May 19 23:42:18 PDT 2004


In the last episode (May 20), Till Plewe said:
> My problem is essentially that freeing large numbers of small chunks
> of memory can be very slow. I have run into this problem twice so
> far.

Do you have a testcase?  The attached program mallocs 1 million
128-byte blocks, then frees them.  With MALLOC_OPTIONS set to jz (i.e.
no filling of freed memory), it takes .184 seconds to free them all. 
With it set to J, it takes 1 second.

CPU: Intel Pentium III (909.96-MHz 686-class CPU)
 
-- 
	Dan Nelson
	dnelson at allantgroup.com
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>

#define NUM 1048576
#define SIZE 128

void *mypointers[NUM];
struct timeval elap;
struct rusage r_s, r_e;

int main(void)
{
	int i;

	printf("malloc:");
	fflush(stdout);
	for (i = 0; i < NUM; i++)
	{
		mypointers[i] = malloc(SIZE);
	}
	printf("done.\nfree:");
	fflush(stdout);
	getrusage(RUSAGE_SELF, &r_s);
	for (i = 0; i < NUM; i++)
	{
		free(mypointers[i]);
	}
	getrusage(RUSAGE_SELF, &r_e);
	timersub(&r_e.ru_utime, &r_s.ru_utime, &elap);
	printf("done. %ld.%06ld\n", elap.tv_sec, elap.tv_usec);
	return 0;
}


More information about the freebsd-questions mailing list