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.
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.
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.
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.
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.