Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ corrupt my thinking, how to trust auto garbage collector?

I use to program mainly with C/C++, that's make me dealing with pointers and memory management daily. This days I'm trying to develop using other tools, such as Java, Python and Ruby.

The problem is that I keep thinking C++ style, I'm writing code as C++ usually written in almost every programming language, and the biggest problem is the memory management, I keep writing bad code using references in Java and just get as close as I can to the C++ style.

So I need 2 thinks here, one is to trust the garbage collector, let's say by seeing benchmarks and proofs that it's realy working in Java, and know what I should never do in order to get my code the best way it can be.

And the second think is knowing how to write other languages code. I mean I know what to do, I'm just never write the code as most Java or Python programmers usually do, are there any books for C++ programmers just to introduce me to the writing conventions? (by the way, forgive me for my English mistakes)

like image 761
SnirD Avatar asked Dec 17 '22 00:12

SnirD


1 Answers

Having a good intuition for memory usage and common leaks is a good thing in Java as well. That memory leaks are impossible in Java is a common misconception.

Someone who ignores careful memory management, will for instance quickly end up with large trees of dangling GUI components, reachable from listener lists in long lived models. (Been there, done that.)

Keep writing your code structurally, and don't "exploit" the fact that you can be lazy.

Another misconception is that the garbage collector is "slow". The algorithms are quite efficient and you should not worry about it until you've profiled your program. A good tool is JVisualVM (bundled with the JDK). That tool will show you CPU-profiling and can help you with possible memory leaks and track down unnecessary small temporary allocations.

like image 164
aioobe Avatar answered Dec 24 '22 01:12

aioobe