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

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!

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

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

VirtualMachineError is parent of OutOfMemoryError and StackOverflowError

Tags: ,

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

Continue reading » Comments Off on VirtualMachineError is parent of OutOfMemoryError and StackOverflowError

Garbage Collection, Reference types and Reachability – Strong, Soft, Weak, Phantom, Unreachable

Tags: , , ,

The Java Virtual Machine has 5 different levels of reachability for an object. Strongly reachable An object is strongly reachable if it can be reached by some thread without traversing any reference objects. A newly-created object is strongly reachable by the thread that created it. This is the reference you know about and use all […]

Continue reading » 2 Comments

HotSpot JVM – Client / Server VM optimization – 260ms vs 0ms!

Tags: , , , , , ,

The Sun/Oracle HotSpot Java Virtual Machine is available for use in two flavors – the Client VM and the Server VM. The VM to be used can be specified by the -server and -client switches passed when starting the JVM with the java command. The default VM when no switch is specified is the Client […]

Continue reading » 10 Comments

Java Synchronization Benchmark

Tags: , , , , , , , , ,

Here is a very simple and straightforward benchmark to demonstrate how synchronization in Java can affect speed of execution to different extents in Java 1.4 and Java 6. In the attached image (click above for full size), you can see that for Java 1.4 the synchronized method needs over 700% more time to do its […]

Continue reading » 7 Comments