Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 67: Lazy instantiation

Balancing performance and resource usage

  • Print
  • Feedback
It wasn't so long ago that we were thrilled by the prospect of having the on-board memory in an 8-bit microcomputer jump from 8 KB to 64 KB. Judging by the ever increasing, resource-hungry applications we now use, it's amazing that anybody ever managed to write a program to fit into that tiny amount of memory. While we have much more memory to play with these days, some valuable lessons can be learned from the techniques established to work within such tight constraints.

Moreover, Java programming isn't just about writing applets and applications for deployment on personal computers and workstations; Java has made strong inroads into the embedded systems market as well. Current embedded systems have relatively scarce memory resources and computing power, so many of the old issues facing programmers have resurfaced for Java developers working in the device realm.

Balancing these factors is a fascinating design problem: It's important to accept the fact that no solution in the area of embedded design will be perfect. So, we need to understand the types of techniques that are going to be useful in achieving the fine balance required to work within the constraints of the deployment platform.

One of the memory conservation techniques Java programmers find useful is lazy instantiation. With lazy instantiation, a program refrains from creating certain resources until the resource is first needed -- freeing valuable memory space. In this tip, we examine lazy instantiation techniques in Java class loading and object creation, and the special considerations required for Singleton patterns. The material in this tip derives from the work in Chapter 9 of our book, Java in Practice: Design Styles & Idioms for Effective Java (see Resources).

Eager vs. lazy instantiation: an example

If you're familiar with Netscape's Web browser and have used both versions 3.x and 4.x, undoubtedly you've noticed a difference in how the Java runtime is loaded. If you look at the splash screen when Netscape 3 starts up, you'll note that it loads various resources, including Java. However, when you start up Netscape 4.x, it doesn't load the Java runtime -- it waits until you visit a Web page that includes the <APPLET> tag. These two approaches illustrate the techniques of eager instantiation (load it in case it's needed) and lazy instantiation (wait until it's requested before you load it, as it may never be needed).

There are drawbacks to both approaches: On one hand, always loading a resource potentially wastes precious memory if the resource isn't used during that session; on the other hand, if it hasn't been loaded, you pay the price in terms of loading time when the resource is first required.

Consider lazy instantiation as a resource conservation policy

Lazy instantiation in Java falls into two categories:

  • Lazy class loading
  • Lazy object creation


Lazy class loading
The Java runtime has built-in lazy instantiation for classes. Classes load into memory only when they're first referenced. (They also may be loaded from a Web server via HTTP first.)

  • Print
  • Feedback

Resources
  • Read Java in PracticeDesign Styles & Idioms for Effective Java by Nigel Warren and Philip Bishop (Addison Wesley Longman, ISBN0201360659) http://www1.clbooks.com/asp/bookinfo/bookinfo.asp?theisbn=0201360659
  • Be sure to check out Concurrent programming in JavaDesign Principles and Patterns by Doug Lea (Addison Wesley Longman, ISBN0201695812) http://www1.clbooks.com/asp/bookinfo/bookinfo.asp?theisbn=0201695812
  • And don't miss More effective C++ by Scott Meyers (Addison Wesley Longman, ISBN020163371X) http://www1.clbooks.com/asp/bookinfo/bookinfo.asp?theisbn=020163371X