Oct 18 2011

Common Causes for Memory Leaks

You’ll always have to deal with memory issues, no matter the programming language. Even with the Java programming language, if the right precautions are not taken, you will have some sort of memory leaks, memory issue, out of memory exception, or heap size problem. I’ve seen two common types of memory issues in every application I’ve worked on.

A common source of memory leaks is global static singleton god object that collects or manages a lot of data, maybe a system cache, object lookup table, service locator, etc. This type of singleton pattern will require other objects to register with it, add themselves to the global pool of objects, but if they are not properly removed, unregistered, when they are no longer needed you will see your memory usage increase over time. I’ve seen this issue when using the callback or listener pattern and the listener object itself holds a lot of other data. This sort of problem is usually relatively easy to identify with a profiler, it will usually be one of the largest objects in your system.

The other, more difficult memory leak to identify, is when you have hundreds of thousands of objects each taking up a reasonable amount of memory. In this case, a single object instance will not take a lot of memory but collectedly the hundreds of thousands of objects can eat up a lot of memory. Here are a few things you can think about when dealing with a small class that spawns thousand of objects…

If you have int types, see if you can change them to short or byte types. Try subclassing if you have any number of properties that most often than not set or are null. Think about lazy loading arrays, lists, and other objects references. If there are many object instances of this class, and any portion of these instances are logically equivalent, think of using the flyweight pattern.


Oct 11 2011

Keep Code Statements Simple

I don’t count my progress by the line of codes but at the same time I don’t take pride by over engineering a solution. Writing code is like writing for a publication, you have to know at what reading level you are writing for. That said, the one type of code statement that gets under my skin is what I call the run-on code statement. A Run-on code statement is one that has multiple method calls in one statement. Here is a made up example of a run-on code statement.

DataManager.getInstance().refreshData(obj.getAsInteger().toString());

In the above run-on code statement there are four method calls. I’ve seen worse. The reason whey run-on code statements are a pet peeve or mine is that if anyone method call fails because of a NullPointerException or some other error it’s difficult to quickly know what segment of the code statement failed. This is also annoying to debug if you want to step into one method out of the four.


Sep 29 2011

Find the Current Working Directory in Java

There are times when you don’t have full control of the location where your Java application runs from. This could happen because the application is installed in a location other than the one recommended by the installer, or because it ran from the IDE, or some other reason. For whatever reason, if you need to find the current working directory where your Java application runs there are two different approaches. The first approach is to use the File class and the current directory symbol to find the current directory. Remember that the single period “.” represents the current directory and two periods “..” represents the parent directory.

   String currentPath = new File(".").getCanonicalPath();

Unfortunately, using the File class throws an IOException. There is another approach that does not throw an exception and returns the same absolute path to the current directory where the Java application is running from.

   String currentPath = System.getProperty("user.dir");

Sep 9 2011

Remove Multiple Null Values From A List in Java

I’ve had situations where I’ve needed a list of foreign keys (fks) that I get from a result set and from that list make additional queries. Sometimes for whatever reason there are null values in the list and I have to remove them. You might had a similar problem where you needed to remove multiple occurring value from a lists in java. There are a few ways you can approach this problem. You you can remove each occurrence of a list element one at a time. The example below removes any null element in the list one at a time.

while(ids.contains(null)) {
	ids.remove(null);
}

There is another approach you can use to remove all instances of a given value from a list in one go. Instead of the remove method you can use the removeAll as in the following code sample.

ids.removeAll(Collections.singleton(null));

Of course, instead of null values you can remove all instances of any given value from a list based on your business needs.


Sep 8 2011

Launch Default Web Browser in Java

For the longest time I’ve used the BrowserLauncher library to open the default web browser to a specified web page from Java. BrowserLauncher is simple to use, just import edu.stanford.ejalbert.BrowserLauncher and call openURL method with the desired website URL.

Since Java 1.6, the JDK has introduced the java.awt.Desktop class to do the same so you don’t need an additional third party jar. The Desktop class has the ability to launch the desktop’s default email client and default web browser given a URI. Here is how you can launch the desktop’s default web browser in Java.

// Launch your default email client with ...
URI email = new URI("mailto:myemail@mydomain.com");
Desktop.getDesktop().mail(email);

// Launch your default web browser with ...
URI url = new URI("http://www.mydomain.com");
Desktop.getDesktop().browse(url);

Jun 27 2011

Where To Download Previous Versions of Java

Earlier this week I received the following Skype message from a co-worker.

[10:10:15 AM] i can not find an where to download official Java 6 update 23
[10:10:21 AM] mostly its from third party sites

I can’t even begin to state how many things I find wrong from the above message. Normally I would just reply with a link to Let Me Google That For You, such as the following search for Java 6 Update 23 download. I decided against being a wise guy, and found the Java archive site because it seen this question come up before for other software packages. Not all, but most software vendors such as Oracle, MySQL, Perforce, and others make available previous versions of their tools or software. It’s usually in a small link at the bottom of the main download the reads software archive, older version, previous releases, or something to that effect.

In the case of Java, Oracle has a Java Technology Products Download page where you can find the older versions of the JDK, JRE, and other Java framework and toolkits.