PocketMatrix forums

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

Determine program memory requirements

Hello all... I'm trying to determine the amount of memory I need to specify as a minimum for running a program I'm working on... I've tried the following:

Code:

MEMORYSTATUS ms;
ms.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&ms;);
TCHAR szOut[22];
swprintf(szOut, L"%d", ms.dwAvailPhys);
wcscat(szOut, L"\n");
MessageBox(NULL, szOut, L"", MB_OK);

I do this at the start of my program, first thing, then after I load all my graphics and sound and whatnot, I do it again. It seems obvious that the difference is the amount of memory my program is taking up during execution (assuming no other memory allocations take place, which is the case).

However, I never seem to get the same result, and the range it varies is substantial. Also, the numbers don't seem to bear any relation to the amount of free memory as reported by various free memory reporting tools.

Am I missing something here? Is this the proper way to determine program memory usage? If not, how is that best accomplished?

Thanks all!
Ignore the physical memory stuff - all sorts of things might affect that. What I'd do if I were you would be to run the memory check from within your program, and use:

dwAvailVirtual
Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.


Which will tell you how much of your 32MB (IIRC) virtual address space you've used.

I would guess that would give a better estimate of what you want than the physical memory side of things. (Too tired to think about it properly at the moment).

HTH,

Ref.
Also note that the OS will dynamically adjust the storage / program memory ratio. For example if you start with 12 MB free program memory and use 10 MB, you may still have 5+ free if the OS adjusted the ratio.

Dan East
It is also worth noting that you can "make" the os recall some memory if needed.
Refractor, I'll try the virtual member you noted in a little while... I did see that, but the description didn't sound quite right... That's probably what I get for thinking! :)

Anonymous, do you have any more information on asking the OS to reclaim memory? I imagine it just send WM_CLOSE messages to running apps, which probably isn't such a nice thing to do from one's program, but I'd like to know the details none the less.

Dan, I did know about the OS adjusting memory, but do you know if that in practice averts failed malloc's? Assuming enough memory exists of course, will the OS always adjust as required to avoid failures?
Well, to some extent. There appears to be a timeout involved, so if the OS can't come up with the RAM in certain amount of time then malloc will fail. That's just based on my own observations and no hard evidence. If you'll note in Pocket Quake 1, I have the main stack allocation occur in a loop (it's a large 8-9 MB allocation). If it fails it retries the allocation several times with a couple second Sleep between tries.

Dan East
I've never looked at the PQ source, but that's a very interesting bit of information that I think will help me. Thanks Dan!