
malloc/new eats up memory
Quote:
>I got a really cute problem with malloc/cute.
>situation: I need to allocate the memory for a LOT of objects/structures at
>runtime
>problem: When I try to use "malloc(1)" / "new char" for about 1000000 times,
>it eats up 200MB+ (!) of memory. When I use "malloc (1000000)" / "new
>char[1000000]" it uses only 1MB. Am I missing something?
When you allocate a block of memory, malloc/realloc has to store
housekeeping information, so that it knows how much memory to release
when you come to free the block. The simplest approach is to allocate
additional space for a single void *, which points to the next
available block, but more sophisticated schemes may be used to improve
performance. Also, because malloc must return a pointer that is
properly aligned for any data type, the block you are returned may
contain padding.
An overhead of 199 bytes does seem excessive, though. I'd expect
something closer to the order of 2*sizeof(long double)-1. Either
a) You're using debug versions of the memory allocation functions (try
changing your compiler options).
b) Your implementation has been tuned heavily towards performance, at
the expense of memory effiency (again, see if there are any options to
control this).
c) Your implementation is horribly inefficient (get a new one).
-- Mat.
--