PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

HELP:Memory allocation problem

I have a strange memory allocation problem which is driving me nuts. I load large images into a tile cache for fast access. I store my image in multiple blocks of memory returned by malloc(). When I later free off the memory I don't seem to get it back again. I am using GlobalMemoryStatus to give me an idea of how much memory is available but it appears that either free() is not working or because of fragmentation free physial memory is being misreported.

The following lines allocate 24 blocks of 64K then immediately free them...

for(i=0; i<24; i++) Ptr[i] = (UINT *)malloc(1024 * 64);
for(i=0; i<24; i++) free(Ptr[i]);

the result should be no change in free physical memory as I've de-allocated all 24 blocks... Unfortunately GlobalMemoryStatus does not reflect this. :?

If I change the allocated block size to 128K eveything works fine.

Anybody any ideas.... is there a way to compact/defragment memory?
Im thinking this should be in the devlopers forum :/
Moved
Sometimes memory handlers are "happier" if you destroy in reverse order of allocation (makes sense if you think about it).

So, try this (or something like it - I'm too brain dead to think right now) instead:
Code:
for(i=0; i<24; i++) Ptr[i] = (UINT *)malloc(1024 * 64);
for(i=23; i>0; i--) free(Ptr[i]);
joshbu [MSFT] · 04 May 2004, 21:04 UTC
So, you're running into a virtual memory vrs. heap memory issue.

When you allocate memory using malloc, 2 things are happening. The first is the operating system is giving some pages to the heap (via VirtualAlloc) and then those pages are handed over to you, with a little heap header on top that tells the heap that you have allocated that block (not necessarily page sized or aligned.)

When you free the block in the heap, the block is marked free and may be allocated again via the same heap, but the virtual memory is NOT released to to OS. The heap hangs on to it's free blocks.

So, you can use the memory again via malloc or HeapAlloc on the same heap, but you can't via VirtualAlloc, and GlobalMemoryStatus retuns what you can VirtualAlloc, hence the disconnect.

If you really want unused pages to be given back to the OS (via VirtualFree,) you can use HeapCreate, then HeapDestroy when you are done with the heap.

Incidenlty that 64K size may also be very close to a magic number, depending on the OS rev (3.0 or 4.2) There is a allocation size, that if you get any bigger, the heap punts the allocation over directly to VirtualAlloc, and hence the free becomes a VirtualFree. Try ramping up the size of the allocation to home in on that magic number.
joshbu [MSFT] wrote:So, you're running into a virtual memory vrs. heap memory issue.

When you allocate memory using malloc, 2 things are happening. The first is the operating system is giving some pages to the heap (via VirtualAlloc) and then those pages are handed over to you, with a little heap header on top that tells the heap that you have allocated that block (not necessarily page sized or aligned.)

When you free the block in the heap, the block is marked free and may be allocated again via the same heap, but the virtual memory is NOT released to to OS. The heap hangs on to it's free blocks.

So, you can use the memory again via malloc or HeapAlloc on the same heap, but you can't via VirtualAlloc, and GlobalMemoryStatus retuns what you can VirtualAlloc, hence the disconnect.

If you really want unused pages to be given back to the OS (via VirtualFree,) you can use HeapCreate, then HeapDestroy when you are done with the heap.

Incidenlty that 64K size may also be very close to a magic number, depending on the OS rev (3.0 or 4.2) There is a allocation size, that if you get any bigger, the heap punts the allocation over directly to VirtualAlloc, and hence the free becomes a VirtualFree. Try ramping up the size of the allocation to home in on that magic number.


Thanks joshbu,

I think you've hit the nail on the head. It wouldn't be a problem if I simply allocatd memory and kept that memory until my program exits, but when I load a different game level I ditch the memory and allocate again for the new level graphics. The problem was that I could load a level once, but attempting to load a second time would fail due to lack of memory!!

I will try allocating my own heap as suggested and see if that improves the situation.
refractor wrote:Sometimes memory handlers are "happier" if you destroy in reverse order of allocation (makes sense if you think about it).

So, try this (or something like it - I'm too brain dead to think right now) instead:
Code:
for(i=0; i<24; i++) Ptr[i] = (UINT *)malloc(1024 * 64);
for(i=23; i>0; i--) free(Ptr[i]);


I did try this and understand the reasons behind it, but on this occasion I knew it was something a little more in depth... cheers