The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: RedXVII on April 05, 2007, 10:17:34 PM

Title: Memory - big chunk or small chunks
Post by: RedXVII on April 05, 2007, 10:17:34 PM
I am allocating some memory to some large arrays (in total these "large arrays" are ~4400 bytes in size). But I have a quite a few of these I need to allocate. Performance speaking, would it be better if I GlobalAlloc the whole lot of them into one large chuck, or GlobalAlloc them seperately into respective small chunks, or would this end up just being the same performance wise?

Cheers  :U
Red
Title: Re: Memory - big chunk or small chunks
Post by: dsouza123 on April 05, 2007, 11:28:56 PM
Too bad the arrays aren't 4096 bytes in size, then they would be match the size of a memory page.

How long are they used ?
The life of the program run or repeatedly created and destroyed,
or can they be reused.

Since they aren't multiples of a page size then allocating many separate ones is less efficient,
each 4400 needing two pages, if one chunk then only one partial page or maybe even none
would be needed.

None if for example 256 arrays of 4400 bytes were allocated.

Are they accessed by variable name or by offset from a base address ?
Title: Re: Memory - big chunk or small chunks
Post by: hutch-- on April 06, 2007, 12:33:23 AM
Red,

If you can organise it and round up the allocated size to ensure it is aligned properly, a single large allocation is a better proposition especially if you allocate an array of pointers to each member of the original allocated memory.

If you go the route of many small allocations I would be inclined to go with HeapAlloc() as it seems to have the legs for this style of allocation. If alternatively you go for a large single allocation GlobalAlloc() using the fixed memory flag does the job fine.
Title: Re: Memory - big chunk or small chunks
Post by: PBrennick on April 06, 2007, 01:15:27 AM
I like Hutch's suggestion to use HeapAlloc. Start with GetProcessHeap, then HeapAlloc space for the array member. Keep track of the count of arrays so if you want them all allocated at the same time, you can do it in a loop. When you are done using them, you can then use a loop to do the HeapFree of all the reserved memory. If you are accessing them one at a time, then consider using HeapReAlloc.

Paul