I am relatively new to assembly language programming, but the language is taking to me quite well :) I've read, followed and understood almost all of Iczelion's excellent tutorials, but I've always wondered... Most projects take up a meg in memory, even if they are template-type blank windows, and I'm wondering why is this? Is there a way to have some controll on the mem usage of the app? I know it's a little extreme, but at times it comes in handy ;) I've tried to mess with the stack size using the /F switch in ML.EXE, to no avail..

I'm using MASM32v8 package. Is anything possible? Mabe an API call to 'squish' unused memory?
Posted on 2003-07-09 02:42:43 by FearHQ
Hi FearHQ,

I'm also quite interested in memory usage.
I think that this rules should be applied if you need your app to be really tiny:

- use the ws:aggresive linker option See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcce/htm/complref_142.asp

- allocate memory from the Heap only

- do as much as you can you own functions instead of the API.
Use standard window controls instead of registering you own window class.

- Load every dll with LoadLibrary and (try to) free it after use.
kernel32, user32.dll,advapi32.dll cannot be freed but the others...

Well, optimize for size, not speed.
I guess it is not a good idea to use ocx controls or dlls(to display
an animated gif for ex.). I saw examples over there where people load mshtml.dll and all the ie stuff to display an animated gif. Of course, it is easiest way but very bad on memory. I remember xnview uses a freeware library to display animated gifs.

The last thing, there is a technique called "delay loading" which permits to load dlls only at the moment you call the function which they contain. An asm example can be found here

I let the Experts correct me :-)
Posted on 2003-07-09 16:04:00 by Prahogi
First of all what program do you use to tell you how much memory each process takes up?
If it is task manager (2k/xp) then you should not be worry, and here is why

Memory usage that is shown for each process is overall allocated memory in address space of that process. Meaning it does not include only memory for your program, it also include memory allocated by system dll's that are loaded with every process like kernel32.dll, user32.dll and others.
I think that this also applies on other program that tell's how much memory takes each processes.
Posted on 2003-07-09 16:27:28 by Mikky
Prahogi: Thanks for the exhaustive info you gave me! Certainely I'll try the aggresive switch ;)

Mikky: I wouldn't expect taskman to include the resources that are shared accross the whole system... But I kow better now. Thanks :)
Posted on 2003-07-09 17:17:57 by FearHQ

- do as much as you can you own functions instead of the API.

You'll end up using more code space. Only real advantage of this is you have the chance to use faster versions (ie, DO use your own string routines instead of kernel32 stuff).

Load every dll with LoadLibrary and (try to) free it after use.
kernel32, user32.dll,advapi32.dll cannot be freed but the others...

Too much mess and not really worth it. Adds more code, and takes longer time. And for DLLs that are used by more than one process, this is entirely worthless, as code pages are shared. You'll end up being less optimal.

Delay-load of DLLs is a nice thing, but you should use it mainly for DLLs that aren't always used, or wont be used until some point when the user needs to perform a special task. Ie, don't delay-load kernel32, user32, gdi32 (etc). But if, say, you have your own help system using mshtml, that would be a good candidate for delay-load.
Posted on 2003-07-10 01:38:21 by f0dder
First of all what program do you use to tell you how much memory each process takes up?

What program would you use ?

I think the simplest is to pop a cmd.exe and write listdlls yourexe.exe
If you see more than 5 dlls (and your app isn't using COM), I think it is a moment to optimize.


I fully agree with your arguments. But what would be your answer to the question ? I remember you wrote a bunch of tutorials on memory but you haven't dealt with "memory usage optimization".

I'm planning to make a test app which will measure its own memory usage and try too limit it with every possible way (at runtime). What would you advise ?
Posted on 2003-07-10 09:45:57 by Prahogi
Actually I am not using any program of this kind, I just particurally readed about this for task manager.

I am not exacly sure what are you trying to accomplish, but I think that trying to manage memory dynamically, when process is loaded is is pretty much waste of time and code.
Really, consider todays machines with 256mb or more RAM, what would you achive if you optimise your program to use say one megabyte less than usual? Not worth of effort if you ask me. 15 years ago programmers were forced to do this kind of tricks but then memory were pretty limited, eg. I know that some games from that time were creating levels by some calculations rather than todays "create level, save it in file and load it in game when needed".
As long as you clealy deallocate memory which you allocate with (GlobalAlloc/HeapAlloc/VirtualAlloc and others) and close handles, there should be no problems.
Ofcourse all this is my humble opinion, if sombody think I am wrong I really would like to hear others thoughts on this.
Posted on 2003-07-10 16:21:49 by Mikky
I wouldn't waste time on dynamic load/unload of DLLs, and I wouldn't worry too much about having a lot of used DLLs (as long as you use them, and they're system DLLs used by other processes).

Spend your time on more important things... reduce _your_ memory usage by using smarter data structures, better handling of your dynamically allocated memory. Choose better algorithms. Write better code.

If you want something that doesn't really matter much but gives somewhat better results in task manager, do a SetProcessWorkingSetSize when app initialization is done before entering your mainloop.
Posted on 2003-07-10 17:43:50 by f0dder