Best Practices Tutorial

Index
chapter 1 | chapter 2 | chapter 3
chapter 4 | chapter 5
chapter 6 | chapter 7

home | developer central

Chapter 5 - Optimizing Java

previous chapter | next chapter

Java is a fairly rich and complex language with lots of built in features; by now, it shouldn't surprise you that some of these features are fast, and some are slow.  A good way to speed up your app is to avoid the slow features when you can. Some Java constructions to be careful of include:

Multidimensional Arrays

When you declare a multidimensional array, you aren't just creating an array of simple variable types; you're creating an array of objects, which all have to be memory-managed and garbage-collected eventually. If you find yourself needing a multidimensional array, consider ways you can store the same information in a single dimension array; the savings in memory cost should more than make up for the extra calculation needed to determine which array value you need to access.

Static Variables

Static variables require special consideration; they should be treated as objects, and destroyed when they are no longer needed, or when the application terminates. Another risk to keeping static variables around is if you use objects or resources as static variables; you will see unpredictable results if your application tries to use an object or resource that was created in a previous instance of itself.

String Concatenation

Java string concatenation is deceptively inefficient, due to the need for allocating memory on the fly; if you need to do lots of string manipulation in a short amount of time, look into the stringBuffer classes, and write to preallocated strings.

Circular references

If you have a memory leak that you can't track down, this could be the cause. If you have an object A that is a reference to B, and B is a reference to C, and C is a reference to A (or somesuch similar construction), this will prevent Java from performing garbage collection on these objects.

WebDriver API calls

Making calls to the Web Driver gives you easy access to some powerful features, but all of those calls come at a cost. Every call to the Web Driver has to go through several interface layers. If there's any code operation that you are doing that you can think of a way to do with less API calls, you should; this will speed up your application noticeably. This is especially true for API calls that return a value (such as the various get() methods). Not only does the return value need to be passed through the same interface layers, get() calls can't be automatically cached in the same way that set() functions can be. The Web Driver can line up set() calls for asynchronous processing, so as to return processing to the calling application before the call itself completes; but, with get() calls, the result of the call needs to be known right away, so your code can't continue execution while your request is being processed.

The Java Console

For debugging, the Java console is extremely slow- as a workaround for this problem, the Web Driver has a built in debug window which you should use instead whenever possible.

In the next chapter, we'll look at ways to optimize the 3d in your scene.


previous chapter | next chapter

�2000 WildTangent Inc. All Rights Reserved. Website Terms & Privacy Statement