Imagine that you have a flat file in csv format. And it has a 100 million rows from which you are about to read data to store and process in your app. The data is in the format (orderId, storeIdentifier, amountDue). What optimization can you do here? Note that the storeIdentifier is going to be [...]
Archive for February, 2011
Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError
Every thread created in a Java program has its own stack space. The stack space used is not allocated from the heap. Infact if you look at the OS report on the memory used by your JVM, you may notice that it is more than what -Xmx parameter specifies. This is because, beside other things, [...]
Type inference – Generics magic for initializing collections in every project
The usual syntax for initializing collections in Java is long and boring. The JDK has a type inference feature that makes the task easier. Such methods should probably have been provided with the JDK in the first place. You can add such methods to a Util class in your application for each type of collection [...]
There is a difference between free space and usable space!
You are working on Java code that involves checking if there is enough disk space available to your Java app to complete some task. You look at the File class for ways to do this. You notice the getFreeSpace() method. And you use it to do your checks for the disk space. Voops! That may [...]
ConcurrentHashSet in Java from ConcurrentHashMap
Problem While you do have a ConcurrentHashMap class in Java, there is no ConcurrentHashSet. Solution You can easily get a ConcurrentHashSet with the following code – Collections.newSetFromMap(new ConcurrentHashMap<Object,Boolean>()) Notes A Set lends itself to implementation via a Map if you think about it. So can actually just use a Map. But that may not fit [...]
VirtualMachineError is parent of OutOfMemoryError and StackOverflowError
Today I noticed that the infamous OutOfMemoryError and StackOverflowError classes are not directly descended from the Error class. Rather, they descend from VirtualMachineError – “Thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.” Nothing terribly useful. But good to know nonetheless for [...]
