Summary
We looked at object generations and saw how easy it was to generate a System.OutOfMemoryException
-type exception. We saw how we can use predictive out-of-memory exception checking to save time by preventing the running of code that will cause this exception.
Then, we moved on to discuss long weak references and short weak references. We learned that strong references are not garbage-collected, and weak references are garbage-collected.
We then looked at finalization and saw how the Finalize()
method will be called on objects that are not disposed of, and that we have no control over when the Finalize()
method will run. Then, we looked at how to implement the IDisposable
pattern and suppress the need for garbage collection to call Finalize()
.
Finally, we looked at the various ways to prevent memory leaks, such as properly disposing of managed resources and unmanaged resources. We also saw how to correctly handle events so that we do not cause memory leaks.
With what...