Best Practices Tutorial

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

home | developer central

Chapter 3 - Managing Memory

previous chapter | next chapter

One of the major features that Java offers is automated memory management; in Java, you don't need to keep track of pointers, or destroy objects when you're done with them. This doesn't mean that paying attention to memory management isn't a good idea; if you leave lots of garbage collection up to the Java environment, it might (and probably will) decide to do it at an inconvenient time, which can produce some nasty skipping in your 3d scene. The basic ways to avoid this are:

Economize object creation

The easiest way to start writing a memory-friendly application is to make sure you're not creating objects where you don't need them. If you're creating a new group every time your player fires a missile, try having a fixed number of groups that you re-use, and just hide or detach the model while the bullet isn't in the air. Also, be wary of API calls that return a WTVector3d object; this creates an object to return this value. If you're using getPosition()/ setPosition() to move objects, see if you can get the desired result by calling moveBy() instead.

Destroy objects when you are done with them

This step is pretty straightforward; you shouldn't keep WildTangent objects around any longer than you need them, and you should always destroy all objects (set them to null) when the page unloads, so that the Web Driver object itself can unload. You can do your object destruction in your applet's unload() function, as in the HelloWorld applet from Chapter 1:

public void unload()
    // Release references to WTObjects from memory.  
    // This is necessary to successfully release the WT object from memory:
    camera   = null;
    stage    = null;
    light    = null;

In addition to obvious WT objects, also be sure to clean out any memory structures that might have WT objects in them, such as hash tables, arrays, or vectors. A good way to see if your app cleans up properly is to refresh the browser: If your memory usage goes up substantially (more than 100k or so) after refreshing the browser, you are probably missing objects when you clean up.

Monitor object count and memory usage

While your applet is running, you can monitor memory usage with a variety of applications- the easiest method would be to use the Task Manager in Windows 2000. More advanced applications are also available, such as Task Info 2000 from .  If memory use is steadily climbing, this is evidence that objects are being created without being destroyed- which will eventually lead to a stutter in the Web Driver when automatic garbage collection occurs. You can also monitor object creation in the Web Driver's debug window while the application is running; if your object count is steadily rising, this could mean that you are creating objects needlessly. 

In the next chapter, we'll look at managing Web Driver art assets.

previous chapter | next chapter

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