Correction: in java 9+, do not avoid string concatenation if the whole string is computed on a single operation (i.e. not in a loop), the compiler and runtime know enough for it to be optimal
Great video with easy to understand explanation. I have a question. I checked my hprof heap dump as I thought my application was causing memory leaks, but it turns out most of the memory is consumed by byte, char and Strings which are not from my application. How do I fix or avoid memory leaks in that case? Your response will be highly appreciated. Thanks
Thank you for your support and feedback 😊 For the issue which you are currently facing. As there is no user defined classes which are causing the issue. You need to check the over usage of Strings in your application. Are you doing a lot of String manipulation? In such cases you may need to use StringBuilder/StringBuffer depending on the nature of your application (Single Threaded/Multi Threaded). Also, look for the connection objects and buffers, they must be closed after use. I will recommend to use try-with-resources if you are using Java 1.7 and above.
A static field, by itself, is not a memory leak. It's only a memory leak if memory is allocated but not released when no longer needed. If you need a list of doubles for the lifetime of the program, then keeping the data in a static field is an appropriate design. If at some point in the program execution they are no longer needed, just call List::clear.
Why the populate method is not declared static, since it is only using static member? In that case to populate the list we will not need to create the object of the class. And when the list is no longer needed then it could be set to null, to let the garbage collector reclain it. It would not need the unloading of the class in such a case.
Yes you can do it that way also, there should not be any issue. But to test the non static list anyway you have to make the populate method as non static.
@@LazzyProgrammer Agreed, The idea is that whereever a method can be written as static(not dependent on instance), it should be declared static and invoked without using an instance.
Soon to be a correction: the finally block is most of the time replaced by the try-with-resources, and the finally block will be removed in some future java release