.Net is a pig

  • Thread starter Thread starter Mike Peretz
  • Start date Start date
M

Mike Peretz

I have tried everything to make .NET return memory to the OS. I use Dispose
and GC.Collect() as much as possible. I understand that if the memory is
available then why not take it and keep it. However, I noticed that memory
is only returned to the OS when the machine is desperate for RAM, but at
that point it is way too late, the OS is swapping and everything becomes
very slow. The other case where memory is returned to the OS is when I
minimize the application. According to Microsoft Documentation, the process
working size is trimmed, causing memory to be cashed or to be returned to
the OS.



Don't get me wrong, I understand that .NET is like an OS and needs to manage
its own heaps and that is not cheap by any means. But! For the love of God,
my little windows application with an Access connection is just consuming
more and more memory every time I click on a new option. On average, it
starts with 20M and can grow to 40M. Nothing, except minimizing the
application seems to work. This can not be acceptable, am I the only one
that noticed this?
 
No you are not. I'm told this is issue is addressed and fixed in Asp.net
2.0. So till then you will just need to code defensively. The memory is not
given back to the OS until the application domain unloads. Even what you are
seeing with the minimization is not a correct representation of the memory
usage assuming you are using task manager. For accurate memory reads you
will need a more potent tool like a memory profiler available from
microsoft.

I've modified my code to get little bits of information instead of large
blocks of data - which is the recommended way. I suggest you do the same.
This keeps memory consumption to a minimum. Also, I recommend aggressive
memory recycling strategies (like nulling out objects etc). It will grow
inspite of the best efforts, but defensive program techniques will allow it
to grow less aggressively.
 
Hi there,

Generally this is not a problem, as when the system needs RAM, it'll
request that apps free up some memory. If you'd like to force this (not
recommended unless you are facing a real problem), you can call Kernel32's
SetProcessWorkingSetSize, passing a process handle (use
System.Diagnostics.Process.Handle), and then -1 for the other two
parameters. This will trim your working set size, and should reduce the
appearance of memory usage.

As Alvin mentioned, v2.0 will have some working set improvements.

-mike
MVP
 
Back
Top