There is a difference between free space and usable space!

Tags:

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

Continue reading » No comments

ConcurrentHashSet in Java from ConcurrentHashMap

Tags: ,

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

Continue reading » No 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 » No comments

Eclipse tip: Add a short cut key to Skip All Breakpoints

Tags: ,

Eclipse doesn’t have a short cut key bound by default to the ‘Skip all breakpoints’ functionality. Here is how you can set one. While you are at it, you probably want to define a few more shortcuts to functionality you use frequently. In Eclipse, go to Window > Preferences > Type in ‘Keys’ in the [...]

Continue reading » 3 Comments

HibernateException – A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance

Tags: , , ,

The logged exception org.hibernate.HibernateException – “A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance: com.onkarjoshi.hibernate.test.Person.children” Your likely problem Looks like you have a Parent entity and a Child entity. The Parent has a collection of Child entities with cascade=”all-delete-orphan”. You are setting a new collection via the setter thus leaving the [...]

Continue reading » 20 Comments

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

Blocked thread with Read Write Lock due to writer starvation

Tags: , , ,

Here is an interesting and simple example of how you can end up with a locked thread in your application and what you can do to avoid it. Task A – Starts at t = 0. Takes a read lock. Runs for 3 seconds. Scheduled to run every 5 seconds. Task B – Starts at [...]

Continue reading » No comments

Spring Transaction advice : AOP proxy vs AspectJ weaving

Tags: , , , , ,

You are using Spring Transactions with Annotations. MyBS.methodRequired has Required propagation. MyBS.methodRequiresNew has Requires_New propagation. … methodRequired (…) { … methodRequiresNew(…); … } Do you get a new transaction if you call methodRequiresNew from methodRequired? You don’t! Not if you are using the default proxy mode via AOP proxies through which transactional advice is applied. [...]

Continue reading » 1 Comment

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 » 9 Comments

Fix ConcurrentModificationException on ArrayList with CopyOnWriteArrayList

Tags: , , ,

You have a List in your application that is traversed or iterated over very frequently but is modified very rarely. Like a ‘Top 10′ kind of list on your home page which is served for every hit to the home page (think hundreds of hits per second) and is updated by an hourly Quartz job. [...]

Continue reading » 2 Comments

Identifying a thread with high CPU usage in a Java app

Tags: , , ,

One of the threads in your app is consuming way too much CPU. Perhaps pegging a core at 100%. How do you find out which thread? Use JConsole. It comes with the JDK. But out-of-the-box, JConsole won’t provide the info we need. You will need to use it with a plugin that gathers and displays [...]

Continue reading » 4 Comments

Debug with Step Filters in Eclipse

Tags: , ,

I got this excellent debugging tip from a colleague at work recently. Most of the time when you are debugging, you want to be looking at your own code. Not code from libraries or the JDK or even the ‘framework’ parts of your own code base. You can configure the Eclipse Java debugger to not [...]

Continue reading » 7 Comments

Conditional breakpoints in Java development with Eclipse

Tags: , ,

Imagine that you have a screen that shows a large list of items with several columns of data for each row. One of the items is showing odd data. You want to step though the code to see what is happening for that row. If you set a normal breakpoint, you’ll have to break and [...]

Continue reading » 2 Comments

How to use a patch jar to override existing Java class files temporarily

Tags: , , , ,

Suppose you have a large application running in production. A production issue has been reported to you. You are not sure what the problem is. You want to add some log statements to the code. Perhaps you want to try out a small fix too – a 3 line change in the code of a [...]

Continue reading » 7 Comments

Kaspersky ate my jconsole!

Tags: , , , ,

The other day I was trying to attach JConsole to a locally running Java application. Every time I tried it, Kaspersky anti virus that was running on my system “detected” a process intrusion attempt by jconsole and deleted (more accurately, quarantined) jconsole.exe. It popped up a message saying- “Process is trying to inject into another [...]

Continue reading » No comments