1

I have a schedule system that generates games into a certain schedule that could be a 1000 games or more. During this alot of processing occurs and object creation. When I run this auto schedule feature my memory footprint for this website goes up significantly. The objects created are plain poco objects and I am using Entity Framework to grab certain data before the scheduling occurs.

My question is how do I make my memory footprint disappear after all this object creation? Do I need to add IDisposable to all these poco classes so they are removed right away?

Update

Found the issue, NLOG was turned on debug and I had a ton of debug statements going on, which in my case kept increasing the memory! So I turned it to debug!

Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • 1
    This is entirely normal. VM usage will go down eventually, the GC is not in a hurry to release it since it doesn't cost anything, it is virtual. – Hans Passant Apr 29 '14 at 21:29
  • `IDisposable` does not remove managed objects from memory. It is used to free resources that are not handled by GC. – Brian Rasmussen Apr 29 '14 at 21:30

1 Answers1

1

Do I need to add IDisposable to all these poco classes so they are removed right away?

Anything that implements IDisposable should have IDisposable.Dispose() called as soon as the object is no longer needed. Typically the best way do accomplish that is with a using statement.

IDisposable is there to allow for expensive resources (think DB connections) and/or unmanaged resources (think a COM handle) to be released as soon as the object no longer needs access to them. Calling IDisposable.Dispose() will release those resources, but will not cause the managed object to be garbage collected any sooner.

My question is how do I make my memory footprint disappear

The .NET runtime will garbage collect when it things it is the appropriate time to do so. Although you can force garbage collection, the runtime will almost always make a better decision on timing than you would.

What might cause the Garbage Collector to be unable to release memory?

If you still have a reference to the objects somewhere, they will not be garbage collected. You can use the memory profiler in Visual Studio to see what is still referenced (therefore uncollectable) after your schedule job runs.

Should you be worried?

Probably not. Even though the .NET Runtime may allocate significant memory, it will usually release it in a very efficient manner if the memory pressure on the system so warrants. Just be sure you are not holding object references longer than needed.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Will adding IDisposable to plain objects and using the using statement remove them quicker? If I have multiple people running this scheduler, that memory footprint will keep going up! – Mike Flynn Apr 29 '14 at 21:09
  • You could always "take out the trash" via: System.GC.Collect(); I've never seen a good reason to do this though. – Mr. B Apr 29 '14 at 21:12
  • @Mr.B: I linked that method, and also linked discussions as to why that is usually a bad idea. – Eric J. Apr 29 '14 at 21:14
  • Agree, Eric. Hard to optimize his code without seeing it though. – Mr. B Apr 29 '14 at 21:15
  • 1
    My memory for this website stays around 350 MB, but when you using the scheduler over and over I had it go up to 1.6GB, and it ran out of memory which tells me the GC didnt do its job??? I am doing acessing any files or images either. – Mike Flynn Apr 29 '14 at 21:17
  • @MikeFlynn: Check out my edit. It may be that you are still holding a reference to the objects, which would prevent them from being collected. If you get an out of memory situation, that is the most likely place to look. – Eric J. Apr 29 '14 at 21:19
  • Ive tried to use memory profiler, but it gives me a yellow screen of death in the website, not sure why. – Mike Flynn Apr 29 '14 at 21:20
  • In that case, just as a *test*, call System.GC.Collect() after your task completes. If memory usage remains that high, you can be fairly confident that you are holding references to the objects somewhere, preventing collection. – Eric J. Apr 29 '14 at 21:24
  • I suppose that is only an optimization issue. When it work in that manner it means that no data or object or something else are disposed or correctly remove.May u show your code in order to see if there's any help that we can provide you? – makemoney2010 Apr 29 '14 at 22:10