Heap dump of a running JVM with jmap

Tags: ,

You don’t have to wait for your JVM to die with an OutOfMemoryError to get the heap dump you get when you use the -XX:+HeapDumpOnOutOfMemoryError flag. Setting that flag is a good thing though – you get a dump after the heap is big and bloated just before the JVM dies making it easier to […]

Continue reading » 5 Comments

Java – The 4 inner/nested class types

Tags:

If you are trying to clear your thoughts around just how many different types of inner/nested classes there are in Java, this should be a quick refresher. All but the first kind are known as inner classes. Static member classes – These will not hold on to a reference to the enclosing class. You do […]

Continue reading » 1 Comment

Double braces initialization in Java

Tags:

This little trick is not very well known and can look like completely new syntax to the uninitiated. But it is actually a form of something you use quite frequently – anonymous inner classes. // Normal list initialization List<String> days1 = new ArrayList<String>(); days1.add("Sun"); days1.add("Mon"); days1.add("Tue"); // Double braces initialization. Compact! List<String> days2 = new […]

Continue reading » 1 Comment

Eclipse jsp jstl error – Cannot find the tag library descriptor for http://java.sun.com/jsp/jstl/core

Tags: , , ,

You get that error for the missing tld when standard.jar and jstl.jar are not found. If you are using maven, adding the following should fix it. <!– standard.jar –> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!– JSTL –> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> Try restarting Eclipse if the error doesn’t go away after adding this. Do […]

Continue reading » 4 Comments

Maven dependency – Local jar file from an external third party to be installed into project repository

Tags: ,

Some time back, I was working on an application that involved payments processing. I worked with a payments company called DataCash. They made available a jar file with their API. Now the app I was working on used Maven for dependency management. Usually, if one needs an API to be included for use in the […]

Continue reading » 1 Comment

Time portion from your Date / DateTime / Timestamp field getting truncated when using the JDBC ResultSet?

Tags:

Problem – You see that from Date-Time value you are saving into your database, the time part is getting truncated at some point. What on earth is happening? You may notice that if you look at the field in your database, you do see the time being persisted. So infact, it is something you are […]

Continue reading » 1 Comment

Sending a SIGBREAK signal to a Java process to do a thread dump in Windows

Tags: ,

In Linux / Unix if you want a Java process to do a thread dump, you can do a kill -3 <pid>. In Windows, doing the equivalent requires sending a Ctrl+Break to the input console which results in a SIGBREAK being sent to the process. But what if you do not have an input console? […]

Continue reading » 2 Comments

Java NullPointerException Ninja – 10 facts you need to know to avoid problems with null

Tags:

1.The instanceof operator is NPE safe. So, instanceof null always returns false. It does not cause a NullPointerException. You can eliminate messy conditional code if you remember this fact. // Unncessary code if (data != null && data instanceof InterestingData) { } // Less code. Better! if (data instanceof InterestingData) { } 2. A null […]

Continue reading » 9 Comments

Crash your JVM by killing the garbage collector!

Tags: ,

My HotSpot 1.6.0_21 JVM crashes if the following code is run on it. And by “crashes”, I mean that the JVM crashes and leaves an hs_err_pid####.log file with the thread dump from the JVM itself – there is no exception from the application code. class Crash { public static void main(String[] args) { Object[] link […]

Continue reading » Comments Off on Crash your JVM by killing the garbage collector!

How to suspend VM on startup when remote debugging your Java app

Tags:

You want to do a remote debugging session on code that runs in the initial few seconds of your app being launched. A problem that you can run into when attempting to do this is that since the code runs right in the beginning, the JVM may rush past it before you have had a […]

Continue reading » 2 Comments

Assigning a reference to itself in Java has no effect, right? – WRONG!

Tags: , , ,

You see this piece of code in your beloved code base…. referenceToSomethingImportant = referenceToSomethingImportant; You immediately delete it since assigning a variable to itself will never do any thing. Of course the compiler or the JIT machinery is always going to optimize it away anyway. Right? Wrong. Oh yes, there might be another thread you […]

Continue reading » 2 Comments

Atomic array classes and volatile arrays in the JVM

Tags: , , , ,

Declaring an array volatile does not do what many think it does! The common, simplistic understanding of the volatile keyword in Java is that if a variable is being modified by multiple threads, it should be marked volatile so that changes can be seen by other threads. (There is more to it that I’ll leave […]

Continue reading » Comments Off on Atomic array classes and volatile arrays in the JVM

6 things to remember about saving memory with the String.intern method

Tags:

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 […]

Continue reading » 2 Comments

Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError

Tags: ,

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, […]

Continue reading » 8 Comments

Type inference – Generics magic for initializing collections in every project

Tags:

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 […]

Continue reading » 2 Comments