2

So I have a program that increases the memory by 5MB per second. Since only 2GB can be allocated per application, I hit my limit at about 5 minutes after I start my program and get a System.OutOfMemoryException.

I have "done my homework" (hopefully good enough :D) and know that after the end of a block, the variables within that block are not disposed.

Ultimately, I need a way to automatically free the memory from variables used in after each block to stop the OutOfMemoryException from occurring.

P.S.: I realize there is a lot of confusion on this topic so if I made a mistake in my question, please feel free to correct me. But mostly, I would like to get this exception out of the way.

EDIT

Alright something weird happened. I copied the entire project from my desktop (where it doesn't work) to my laptop. And apparently, it works just fine on my laptop! The memory only increases MOSTLY by 500 KBps and is automatically freed 1 second later. I have no idea why, it's exactly the same project and no code was altered.

Anyone know why this happens?

user1687431
  • 29
  • 1
  • 5
  • Post some code. Are you operating on files, dataabase or com's? – cichy Nov 19 '12 at 09:11
  • 6
    If your program continuously uses an additional 5MB every second, you are doing something very, very wrong. Post your code. – Jonathon Reinhart Nov 19 '12 at 09:12
  • Have you tried calling `System.GC.Collect()` explicitly? However, I still agree with @JonathonReinhart - something is very wrong with your general approach. – user Nov 19 '12 at 09:16
  • Very well, I'll see what code I can place. Give me some time, I'll try to find the objects that take up the most memory. – user1687431 Nov 19 '12 at 09:17
  • Post your code so we can give an appropriate advice. Using "using" and IDisposable is one pattern you can use only if you are using resources that need to be managed/disposed of manually. Also note that calling GC.Collect is not not recommended as this utilizes processor time. – rro Nov 19 '12 at 09:49
  • Updated my post. This is a rather interesting twist of events, I do say. – user1687431 Nov 19 '12 at 10:00
  • Not that surprising, you're running on another system. I assume you have not configured the GC mode, in which case your application will use the concurrent workstation gc mode (assuming none of the systems are single-core). This GC mode is not deterministic and have problems with high-load scenarios. Try using server GC, or non-concurrent workstation GC? – Christoffer Nov 21 '12 at 22:03
  • Oh, and of course the .NET versions matter as well. Some builds of the .NET 4.0 CLR have a concurrency bug in the garbage collector, try using .NET 4.5 if you don't do that already. – Christoffer Nov 21 '12 at 22:05

5 Answers5

1

Use these variables in a using statement and call GC.Collect() when done

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • I know, but I have a LOT of variables and manually placing them in Using statements will take me more than a few weeks, trust me. But thanks for your comment :) – user1687431 Nov 19 '12 at 09:13
  • Garbage collection is still non-deterministic, however. `using` only limits the scope (both time and code-wise), it does not force garbage collection at the end of the block. – user Nov 19 '12 at 09:14
  • you can refactor it to have them in Container objects which you then use in a using statement. – CloudyMarble Nov 19 '12 at 09:15
  • @Michael Kjörling he still can force collection procss after releasing resources. – CloudyMarble Nov 19 '12 at 09:24
1

In my experience, the most common way to leak memory in managed code is holding on to references for too long, alternatively not realizing how references are handled. A live reference will prevent garbage collection, no matter how well your code is disposing stuff.

Here is a decent article on how to figure out just what is leaking and where those references are, and you probably want to read up on the 'additional background' links as well.

Christoffer
  • 12,712
  • 7
  • 37
  • 53
1

Use ANTS profiler or CLRProfiler to determine which objects are taking up space and which methods are creating and holding references these object.

rro
  • 619
  • 5
  • 22
0

probably this discussion will help you to understand what is actually happening regarding memory management in .net Finalize/Dispose pattern in C#

and just in case, this is another post on how to dispose objects: the correct technique for releasing a socket/event/ummaged code with the dispose/finalize pattern

Community
  • 1
  • 1
Jorge Alvarado
  • 2,664
  • 22
  • 33
0

Maybe your objects should implement IDisposable so you can clean up resources when you no longer need them. This MSDN article shows how to implement it:

Digging into IDisposable

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86