5

We have a project that is getting an "Out of memory exception". I'm trying to debug this memory leak issue. The problem is that the production machines have about 1-2 GB of memory and the development machines have 6 GB. And I am finding it very difficult to reproduce the crash.

Is there a way in visual studio that I can reduce the amount of memory allowed to be allocated to a debug instance?

Tal Even-Tov
  • 153
  • 2
  • 11
  • there is a [question](http://stackoverflow.com/questions/8966639/set-maximum-memory-usage-c-sharp) that is about this issue, but they didn't find solution either. – Rafal Jul 12 '12 at 09:29

1 Answers1

1

The amount of RAM in a machine has nothing to do with an OutOfMemoryException. You'll get that exception when the process runs out of virtual memory, failing to find a hole in the address space that's big enough to fit an allocation request. That usually happens when the VM size of the process starts getting close to 1.5 GB on a 32-bit machine.

Limiting the amount of virtual memory space is easy enough, just create a bunch of byte[] arrays at the start of your program and store them in a static variable. This doesn't help diagnose a memory leak at all, it merely trips the exception quicker. Use a memory profiler to find the real problem.

And do consider the possibility that this isn't a leak at all, it's not that easy to leak with a garbage collector. But just a side effect of your program processing and storing a lot of data. Which is trivially solved with a 64-bit operating system, it provides oodles of virtual memory space, limited only by the maximum size of the paging file. Not RAM.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks, I'll give it a try and see. We do think it is a memory leak somewhere. We are using a WebBrowser control to view various web pages and pdf files in a the application. We're starting to think it may be that webbrowser control since the client machines are running IE 7 or 8 and the more the pages the clients open the more likely a crash will occur. – Tal Even-Tov Jul 12 '12 at 13:16
  • Hmya, that's certainly a fruitful source of leaks. A .NET memory profiler isn't going to be helpful, browsers leak unmanaged memory. If you can't control what pages are being browsed then making restarting the program as painless as possible is about the only decent workaround. – Hans Passant Jul 12 '12 at 13:21
  • I'm going to try put together and propose a different approach for document viewing. Unfortunately, the project is for a major government department's complete operations so I'm going to have to work with what I've got. Perhaps I can find another (hopefully lightweight) all purpose document viewer. Or create one myself. Thanks for the advice. – Tal Even-Tov Jul 12 '12 at 14:22