Memory management WAS programmer’s responsibility
When I used to program a “lot” in C/C++ one of the things that need to be take care of was memory management. I used to read article like the one on C++ User Journal about the “The Rule of Three” or rule of thumb like if you dynamically allocate memory you need the Rule of Three.
I was always told memory management is your responsibility.
The Pragmatic Philosophy
“Another key to their success is that they take responsibility for everything they do.”
The Pragmatic Programmer: From Journeyman to Master By Andrew Hunt, David Thomas
This approach of memory management is usually claimed as the root of all evil in these languages. Major errors are blame due to this and rightly so.
“This seemingly simple paradigm has been one of the major sources of programming errors. After all, how many times have you forgotten to free memory when it is no longer needed or attempted to use memory after you’ve already freed it?”
http://msdn.microsoft.com/msdnmag/issues/1100/gci/
Memory management STILL IS programmer’s responsibility
Then came Java/.NET with (false) claim that memory management is not programmers responsibility.
“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“
http:// java.sun.com/j2se/reference/whitepapers/memorymanagement_whitepaper.pdf
There are various Garbage Collection algorithms and each platform implements its own. Java for instance uses a Generational based Algorithm that uses different approach for each Generation of Objects. But the main method is to maintain a root set of statically allocated global objects. When times come for garbage collection each object in this root set is traverse to find all the objects that can be referenced from it. All the objects that can be references are added to an object graph. The objects that are not in the graph are one that can be garbage collected as there is no way to access them.
Off course this a simplified explanation as there are other problems like object references from one generation to another generation. Because each generation is garbage collected separately. So garbage collector has to do lot more than what I have said like maintaining references from one generation to another generation objects.
So I do not have to take care of memory management it is responsibility of Garbage Collector.
I do not trust Garbage Collector
I do not trust Garbage Collector and I have reasons for it. I have programmed in C/C++ and Java/.NET and most recently looking into Ruby and Lisp.
But I have seen
java.lang.OutOfMemoryError: Java heap space
and
System.OutOfMemoryException
errors more than memory corruptions and memory leaks using C/C++.
When you try to find out the reason for these exceptions. You are told that “you are not managing your memory properly”.
What!? All the developers/creator/fanboy of the languages like Java,.Net and Ruby all are lying to me! Memory management is STILL my responsibility.
Problem caused by this “lying”
Now due to lying about this important fact, everyone writing code in these languages never think about memory management. That’s the reason we see so many Out of memory problem in such languages.
Sometime back I was looking at the an application that was develop long time a go and now was having memory issues as the number of user has increased. The problem came out to be there were some thread specific object that deep down the object tree were still holding references to objects that were no longer used. So they could not be garbage collected. The problem was not visible when the number of users was small. But all hell broke as number of users increased.
When I asked programmer of that application why such things was ignored he said “does not this language take care of memory it self”?
What disappoints me is that even new languages that are built on top of experience from .NET and Java (for example most hyped language these days Ruby). There creator and supporter are still spreading the lies!
My request is to these people to stop spreading the lies and tell the programmers that:
“Memory management is still your responsibility”
Beyond Garbage Collectors … coming up in next post