A Funny Java Flavoured Look at the World

Friday, April 14, 2006

Is Memory Management in Java Garbage?

A friend of mine who was learning C# commented he felt C# was a more powerful because you had to memory manage yourself and it took more effort to create and destroy objects. Although you could get create an awful mess (forgetting to destroy objects etc) he said it felt like you had more power and control and this lead him to believe that C# is a more powerful programming language than Java.

There are two schools of thought with how this is done in Java.

1. Java automatically does the Garbage collecting and probably does a better job than most developers

on the down side
2. By auto Garbage collecting it takes away an opportunity for developers to optimise garbage collecting and auto Garbage collecting can be seen as a constraint not a benefit.

I am of the opinion that in the most part it is a benefit because it simplifies programming in Java and is probably more efficient at garbage collecting than self regulated Garbage collecting, unless people spent time researching the best way to garbage collect.

On the flip side I have only felt the desire to be able to command the Garbage collecting and it was when I was creating a lot of reports that were images and took up a lot of memory. I would have like the ability to garbage collect the images as soon as they had served.

The downside of the automated memory management is I as a Java developer tend to have a lazy attitude about creating and destroying objects because it is taken out of my hands, I don't take any responsability for it. The negative effect of Garbage collecting being done automatically is Java programmers don't write programs with memory usage as a concern which might result in memory inefficient programs.

Most the time this doesn't really effect me as Java developer because there is enough memory for 99 percent of most java applications. Of course when we get to the point where we need to create a Java program with efficient memory management, we aren't sure how because we never practice those skills. At the moment my memory manage skills involve trying not to make create objects where I don't need them basically using good design, e.g. using singletons and static classes, lazy loading and nulling variables that aren't needed. The problem with the automatic garbage collecting is I can't guarantee that these measure make any difference.

I think overall I'm glad Java manages the memory because they probably do a better job than me but I would like to have more knowledge of creating better memory managed Java programs.

8 Comments:

  • Um, did he mean C++ or something? C# has garbage collection built in.

    By Anonymous Anonymous, at Sun Apr 16, 04:36:00 am 2006  

  • I saw a recent study about drivers and cars where they took some normal people and put them in cars with "good" performance from today and in some cars from 30 years ago and have them drive a course. First they drove the modern cars with no issues. Then they drove the older cars and most people handled the cars about as well as a legally intoxicated person would. The difference was the modern features like automatic traction control that made driving "easier".

    Java's garbage collector is like those features. It allows more people to be better programmers with less effort. Now just like a professional racecar driver probably doesn't want the car's traction control automatically applying the break in hard turns, there are situations where you don't want a garbage collector. But 99.9999% of the driving or programming in the world aren't those situations.

    By Anonymous Sandy McArthur, at Sun Apr 16, 05:53:00 am 2006  

  • I think you are right and I don't really believe there are many if any occaisons where you would need to take control over the garbage collection. but I also do think that Java developers code would benefit if they had to be mindful of creating and destroying objects, so when they finished using them they would get rid of them.

    but overall I'm glad I don't have to worry about it

    By Blogger Hosky, at Sun Apr 16, 07:57:00 pm 2006  

  • I once thought the way your friend thinks (having a C++ background) - after moving to Java everything felt so taken-out-of-my-hand. Well, after 6 years programming in Java and having read quite a few interesting articles I came to realize how ingenious the Java GC mechanism really is - if you know, how to take advantage of its features.

    And I believe that this is the main problem (as you already pointed out): Java's memory management is not "automatic" as in "I don't have to take care of anything". We still need to nullify variables we don't need anymore, clear maps, lists and such after we are done with them. And we can rely on the fact, that if we obey these rules and we run low on memory, the GC kicks in, suspends our program for a short period of time (depending on the GC settings) and after that we're done. I saw a lot of C++ programs that had memory leaks (often these were my own :-) ) and it was a real pain to get rid of them (using tools such as Compuwares/Numegas DevPartner and such). I am glad I am "only" responsible for careful memory consumption, but not for calling "free" on any object I don't need anymore :-)

    By Anonymous kaylen, at Fri Apr 21, 08:06:00 am 2006  

  • Hey,

    you might want to read up these articles to get your objects garbage collected in an efficient manner:
    http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html

    http://java.sun.com/developer/technicalArticles/ALT/RefObj/

    By Anonymous Pattern Recognizer, at Fri Jun 09, 10:29:00 am 2006  

  • Please get your facts straight.

    C# and other .Net languages manage memory in almost the same way Java does.

    Your friend might be setting his objects to null but they still occupy memory until GC comes and cleans up.

    .Net is just a Java ripoff. That happens to bind you to windows.

    By Anonymous Anonymous, at Mon Jun 26, 01:22:00 pm 2006  

  • "We still need to nullify variables we don't need anymore, clear maps, lists and such after we are done with them."

    This is total crap. I can't believe it's 2006 and people are still writing stuff like this.

    You don't need to clear lists or maps. You just need to clear references to the map or list and only in certain circustances. Usually (well, almost always) you should just let things fall out of scope.

    By Anonymous Anonymous, at Mon Jun 26, 08:52:00 pm 2006  

  • "On the flip side I have only felt the desire to be able to command the Garbage collecting and it was when I was creating a lot of reports that were images and took up a lot of memory. I would have like the ability to garbage collect the images as soon as they had served."

    You know that you can call System.gc() to 'suggest' that the VM run a collection. If it really not a good time to do it, it might ignore you but that's really better isn't it? Or would you rather your ap become unresponsive because you felt it was important to be incontrol?

    By Anonymous Anonymous, at Mon Jun 26, 08:55:00 pm 2006  

Post a Comment

Links to this post:

Create a Link

<< Home