Hi, I could need some advice how to minimize memory usage
in my program... I really don`t care if the executable gets 1Kb
larger or if it runs somewhat slower --just minimize memory usage.
Posted on 2001-08-01 07:04:28 by WinCC
you know, it depends on what your program is doing. if it allocates memory or stuff like that. i'm not sure about that, but if your program consists of a normal window that's doing something, you don't have much chances to reduce memory usage. and i think memory usage isn't that high in this case.

so if you really want to reduce memory usage, tell us about your program more in detail.

Posted on 2001-08-01 07:24:46 by NOP-erator

It going to have a bit to do with what you are doing with the program, ther are many things you can do if the memory footprint of the program is critical.

Set the stack size in the program so that it is no bigger than you need to run it. Use dynamic memory, GlobalAlloc(), OLE string and similar and carefully deallocate it once it is used so that you don't have unnecessary overlaps of memory usage. Try and keep data allocated in the .DATA section to a minimum as it sits in memory all of the time the EXE is loaded.

If the program can be broken up into smaller parts, try delegating parts of it to DLLs and call them only when you need them with LoadLibrary/GetProcAddress. This keeps you memory demand a lot lower as it is mainly used for transient demand, not loaded all of the time.

Let us know a bit more about what you are doing and I am sure someone can help you if it can be done.


Posted on 2001-08-01 07:29:00 by hutch--
Hi , here is some info about my program.

-- it read cpu temperature from my motherboard it send you an alarm or shutdown the computer in case of high temp.

-- it has 2 threads one for the inputs and one for calculations

-- it minimizes to systray and when the mouse is over the icon
it shows the temp.

-- it has a window for inputs -- is a dialogbox better ?

this is one of my "problems"

the worker thread does this

invoke lstrcpy,addr note.szTip,addr temptip
invoke Shell_NotifyIcon,NIM_MODIFY,addr note

this uses a lot of memory I don`t get back .

I think of this as a "set and forget " program and therefore it should consume as little memory as possible.
Posted on 2001-08-02 00:21:38 by WinCC

I suspct that you problem is more related to processor usage than memory usage, what you are doing should not use all that much memory. The difference between a window and a dialog is not enough to worry about, both build in minimum size code.

How you detect the mouse over the tray icon will have a lot to do with how much processor time you use, the other is your polling rate to determine the processor temperature, this can take a lot of processor time if you run it too frequently.

Probably setting a timer at a wider interval will help so that you don't poll it too often.


Posted on 2001-08-02 01:42:40 by hutch--
I poll every 3 seconds and taskmanager shows 0% cputime

When the program starts and anf sits in systray it uses ~140kb

memory and when I put mouse over tray icon it raises to ~210kb.

how do I get these 70kb back when moving the mouse away ?

has this something to do with the NOTIFYICONDATA structure

to do?
Posted on 2001-08-02 04:14:25 by WinCC

Your specs show that it is not CPU time that is the problem, I would try the stack reserve/commit parameters in the link options and start at about 100k eack and progressively reduce it down until it crashes with a stack fault. This will get some of your memory usage down.

It may depend on how you are measuring memory usage, I would be surprised if an app of the type you are writing used much memory.



Posted on 2001-08-02 05:16:51 by hutch--
Playing with the stack won't really help you much, only the committed
part of the stack. Why? Because the non-committed part of the stack
is only reserved, not committed, so physical memory pages will not
be used for it.

WinCC, *how* do you poll? Hopefully a WM_TIMER... and about the
increasing memory usage when you move the mouse over the tray
icon... does this happen only once, or does the memory usage *always*
increase each time you move the mouse there? And how do you measure
the amount of memory used by your application? The only figure you
should *really* be interested in is the committed, non-paged-out,
non-shared memory of your own application, not including system
DLLs etc.
Posted on 2001-08-02 20:58:03 by f0dder
Hello I`m happy again ---?ve got my "lost memory "back...

invoke lstrcpy,addr note.szTip,addr temptip
invoke Shell_NotifyIcon,NIM_MODIFY,addr note

invoke GetCurrentProcess <---- new line
invoke SetProcessWorkingSetSize,eax,-1,-1 <---- new line


just put this code there and on a couple of other places and it

did it.

now I?m down to 112k memory usage.

all thanks to good old MSDN and the win2k Performance monitor
Posted on 2001-08-03 13:37:12 by WinCC
SetProcessWorkingSetSize ... don't use it if you actually have memory
leaks... it will not take care of those. And it should only really be
necessary to call SetProcessWorkingSetSize once. And you should
still only care about commited pages, or "physical memory pages
Posted on 2001-08-03 15:01:21 by f0dder

this function works for me therefor I use it.

you may call this a "hack" but I get my memory back and it seems to work for me.

I`m sure there are other functions to use but I don`t know them

If you have another better solution please tell me.


from MSDN


"When you increase the working set size of an application, you are taking away physical memory from the rest of the system. This can degrade the performance of other applications and the system as a whole. It can also lead to failures of operations that require physical memory to be present; for example, creating processes, threads, and kernel pool. Thus, you must use the SetProcessWorkingSetSize function carefully. You must always consider the performance of the whole system when you are designing an application. "


I read this as "working set" is all about physical memory.

please tell me if I?m wrong with this.
Posted on 2001-08-03 16:05:58 by WinCC
As far as I know (and I might be wrong...), the workin set size is
how much memory is available to a process. This probably affects
the whole system, so trimming the process working set size is not
a *bad* idea. However, it should not be used as a replacement for
proper memory handling :).

If you want me to, I can have a look at your program and try to
find the "real" error, and see whether your memory increase really
means anything. I can *imagine* a possible cause of your memory
size going up when moving the mouse over the icon (but this is
all guessing):

When you call shell_notifyIcon, you're calling a function in shell32.dll.
You're probably not using much other stuff from shell32. So, it's likely
that this causes "some" pages from shell32 to be committed... then
again, DLLs are loaded in the shared memory pool, and I haven't
done all that much tracing around inside the win32 memory management :).
Posted on 2001-08-03 16:18:29 by f0dder