Memoryhandling.

  • Thread starter Thread starter Glenn Nilsson
  • Start date Start date
G

Glenn Nilsson

Hi!

If I create a new project and build and run it, it will (on my machine)
take 14+ Mb of the physical memory, and about 7 megs of the virtual.

If I then minimize and restores the form it will be at about 1.5/4 megs
instead. Why is this, and how can I programmatically achieve this? I
tried GC.Collect() just for the hell of it, since I don't know exactly
how the memory-handling in .NET is built up, but it didn't do any good
at all.

If I create an empty project with just a [STAThread], nothing more, no
forms, no NotifyIcons, no nothing, it will still use 14 megs, and I
can't even minimize and restore anything to free upp memory.

Ok, maybe the allocated memory gets freed when it needs to be, but it is
really annoying to look at how much memory every .NET-application takes
in the Task Manager.

If I create a small application that only resides in the systray, it's
using more memory than a rather big application written in C++ for example.

What is the secret on how to minimize memory-allocation in .NET?
I tried (not so hard) to find some articles on the net on this subject,
but the search results gets filled up with to many irrelevant hits,
please point my to good sources on this.

/Glenn
 
Glenn Nilsson said:
If I create a new project and build and run it, it will (on my machine)
take 14+ Mb of the physical memory, and about 7 megs of the virtual.

If I then minimize and restores the form it will be at about 1.5/4 megs
instead. Why is this, and how can I programmatically achieve this? I
tried GC.Collect() just for the hell of it, since I don't know exactly
how the memory-handling in .NET is built up, but it didn't do any good
at all.

The minimise/restore behaviour is nothing to do with .NET - it's just
the working set behaviour. Try doing the same with, say, Outlook and
you'll see the same behaviour.
If I create an empty project with just a [STAThread], nothing more, no
forms, no NotifyIcons, no nothing, it will still use 14 megs, and I
can't even minimize and restore anything to free upp memory.

Ok, maybe the allocated memory gets freed when it needs to be, but it is
really annoying to look at how much memory every .NET-application takes
in the Task Manager.

If I create a small application that only resides in the systray, it's
using more memory than a rather big application written in C++ for example.

What is the secret on how to minimize memory-allocation in .NET?
I tried (not so hard) to find some articles on the net on this subject,
but the search results gets filled up with to many irrelevant hits,
please point my to good sources on this.

Most of the memory is taken by the CLR and the framework libraries.
Yes, it's quite a lot - but the difference between a big app and a
small app is only going to be roughly the same as it is in C++, it's
just got a larger overhead to start with.
 
Hello Glenn,

Thanks for your post.

The virtual memory manager will keep a working set of memory resident in
the process whenever the process is started. When the application is
minimized, the operating system will trim the working set for the process.
If you want to achieve the same effect of trimming the memory usage, you
can explicitly call the SetProcessWorkingSetSize() API while passing "-1"
for both the dwMinimumWorkingSetSize and dwMaximumWorkingSetSize
parameters. Please refer to the following articles for detailed information:

PRB: An Application's Working Set Is Trimmed When Its Top-Level Window Is
Minimized
http://support.microsoft.com/default.aspx?scid=kb;en-us;293215

SetProcessWorkingSetSize
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/setprocessworkingsetsize.asp

If it's a .NET managed application, you should use Platform Invoke to call
unmanaged API:

Consuming Unmanaged DLL Functions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingunmanageddllfunctions.asp

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top