Tech Tips Digest: 2
Hope you liked the first Tech Tips Digest yesterday.
I'll be covering every five Tech Tips in each blog post.. And since I'm way behind, today I'll be posting the second part.. Perhaps tomorrow if I had enough time I just might post the third part as well :)
Previous Tips: Tip No. 1
Tech Tip No6: Don't forget to release resources such as Files, Sockets, etc... Sharing is caring, other services might want them
I had eventually managed to find the main source of the memory leak. It was simply the fact that connections to a middle-ware queue system were never disconnected!
Closing connections to files, sockets, database, queues ... etc (The list can go on!), is one of the most important aspects engineers should pay attention to, in order to avoid data corruption and memory exhaustion.
Engineers working with C or C++ know this for sure, others working with Java might think that garbage collection facilities in the runtime environment provide a fool proof measure that they neglect such important actions.
Java Sample suggestion a possible approach to proper handling of files
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileWriter("/tmp/file.txt"));
writer.println("Data");
} catch (IOException e) {
// Log the exception
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
Tech Tip No7: Haven't you heard the news, they've already invented the wheel! Re-use and create genuine modules
Tech Tip No8: Read and understand enough design patterns, they'll help you solve many basic and advanced problems
Tech Tip No9: Always do proper unit testing. If you're using Java like I do, you may want to consider JUnit it's very easy to use
I'll end up today's digest with this simple tip. Have you ever come across some code that really didn't make sense!
For instance, the '==' equality operator in Java simply compares references and not the content of the objects, so it would yield wrong results when used on String objects for instance.
Also, why use eager boolean operators when most of the time you only need the short circuit version. that's much faster.
Finally, I encourage you to read more and explore the source code of others. In my own humble opinion, that is one of the most important sources of knowledge you could ever get.
There is plenty of brilliant open source projects out there with very brilliant engineers working on them, download the source code and read on.
Comments