-2

Is it possible to occur an out of memory exception in runtime even with the garbage collector?

I was asked about that, but I wasn't sure about it.

Otacon
  • 332
  • 2
  • 4
  • 9
  • 4
    It is possible. GC is not to help you fitting all your data into the memory, but to allow you not care of memory (de)allocations – zerkms Jun 24 '13 at 01:49
  • http://stackoverflow.com/questions/597499/why-am-i-getting-an-out-of-memory-exception-in-my-c-sharp-application – OldProgrammer Jun 24 '13 at 01:52
  • 4
    If it was not possible for such an exception to happen then why would there be the exception in the first place? – Eric Lippert Jun 24 '13 at 02:36

3 Answers3

4

Yes, if you use too much memory, that exception occurs.

The garbage collector just gets rid of memory you'll never again access anyway.

... that answer was so obvious, perhaps you asked something different than what you meant? If so, please clarify.

mcmonkey4eva
  • 1,359
  • 7
  • 19
1

Yes, it did happen to me before.

It's pretty obvious: If you use too much memory, then this exception is triggered.

The garbage collector is merely getting rid of data you can't access anymore and doesn't raise the amount of free memory in a magic way.

Jan Berktold
  • 976
  • 1
  • 11
  • 33
  • Hey guys, it's a valid answer for the question asked, +1 – zerkms Jun 24 '13 at 01:52
  • @zerkms, while the answer is a boolean, I think an explanation should go along with it. Just saying "yeah, it does" doesn't constitute as an answer. – gunr2171 Jun 24 '13 at 01:54
  • @gunr2171: it does. At least it will teach OP to ask a better questions. It's not the answerer's fault. – zerkms Jun 24 '13 at 01:57
0

The Garbage Collector only declare "garbage" objects the Application Root does not refer to anymore. If the managed heap is full, and every object in it is still referenced by the Application Root (meaning the object is still reachable by the application), you will get an OutOfMemoryException.

I strongly recommend you read this to know how GC works. The part about the Finalize method is also pretty interesting.

Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55