views:

84

answers:

6

Hello All,

I just wanted to know which language has better memory management among C,C++ and Java,why is it so and it is based on what criteria?

I know that Java uses garbage collection for freeing memory and C uses DMA functions.Does this make java better at memory management since it's handled automatically? I do not know C++ so I don't have much idea there,though I know it uses destructors and delete.

Any suggestions/ideas will be grately appreciated.

+4  A: 

Java has memory management. C and C++ don't, so it's memory management is a function of the programmer.

John Smith
I'm pretty sure C and C++ handle the stack for you. Or do you find yourself doing a lot of `alloca()`?
Chuck
alloca() still doesn't srew up the stack it will be fine after return.
rerun
+1  A: 

The problem with Java is that since it does the garbage collection automatically, and you can only induce it to happen, you cannot free resources at the exact moment you want them to be freed. The advantage is that you do not have to worry about objects being left behind, because they won't affect your memory in the long run (as long as you don't keep a reference to them, of course).

For optimal memory management, I would recommend C or C++, even though between those two, I would say C++ because of the high number of features it has. As for particular arguments regarding the memory management between C and C++, I do not know. In any case, the fact that they allow you to treat things in a much more controlled and customized way, means that you must not relax and forget to do that management yourself.

Hope that helps.

Luis Miguel
+1  A: 

As for internal memory management, Java has the best of the three, since it automates disposing of objects.

If your question aims at performance, C or C++ would be a better bet. You would have to do all of the memory management yourself, but at the same time wouldn't have to wait for a Garbage Collector to do it's job.
IMO it all depends on your approach: If you want to optimize your Application for Performance, go C or C++. If you don't want to worry about memory management yourself, use Java.

Semyazas
+2  A: 

Thats a apples to oranges question in my book. C/c++ don't have memory management at least not in the language thats your job. That being said java will allocate and destroy memory for you all the live long day but at the cost of control. For the standard business app this is not at issue. You are going to load some bloated 3rd party code either way, but when it counts you have more power in c/c++. You also have more power to shoot yourself in the foot.

rerun
A: 

Java and C# have both garbage collection. This is a good thing because the programmer has less problems with memory management, and can concentrate on other problems. In C and C++ you must manually manage memory - for this you need much time and patience and experience. JVM's garbage collector is fast enough, hence you almost don't feel the difference between time execution of C++ programs vs Java programs(C++ is supposed to be faster than java).

Csaryus
A: 

Programmer managed memory in c and c++ is the root cause of many software bugs in programs written in those languages. This is one of the main reasons modern languages like Java and C# have garbage collection.

jeffo