Don’t trust garbage collectors

I recently read This blog post and the author has a nice point about garbage collectors. When we program in languages with that feature we should not blindly trust the automatic memory management for everything. Most of the time it is ok, but sometimes you have to think about memory management in those languages too.
In the blog post Tahseen Ur Rehman gives an example with a thread which was holding some references to the objects so they could not be deleted, so the users were getting OutOfMemoryErrors. The author also warn us that we should not trust the lie

“In some programming languages, memory management is the programmer’s responsibility.
… …
An alternate approach to memory management that is now commonly utilized, especially by most modern object-oriented languages, is automatic management by a program called a garbage collector“

There is also a nice example in Effective Java with the same point. So next time when we want to do something smart with the language, we should think if it is ok for the garbage collector and it will be able to do it’s job. I find that most of the time those kind of errors are found in concurrent programs and hacky arrays.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>