Articles Archive
Articles Search
Director Wiki
 

Memory Managment in Director

June 25, 1998
by Alex Zavatone

Many times I have been called in to work on a title at the last minute because the developer was "having problems with the title" and find that there was no memory management being used in the title at all. Now this is not much of an issue with much shockwave movies but with a CD rom title, it can be a major problem, especially if you're using 16 bit, 24 bit graphics or large sounds.

The concept behind memory or asset management, is to only load what you want before you need it and make sure that the stuff you do not need is not in memory. That's pretty much it. The problems of not using memory management are many and can cause some of the following problems:

  1. running out of memory and sound not playing.
  2. having to fetch media off the CD at the wrong time and getting poor performance.
  3. skipping quicktimes.
  4. running out of memory and the cd quits.
  5. shockwaves that run out of memory on the Mac.

See, Director without any custom memory management will load media depending upon the following:

  1. your preload setting in the movie cast preferences (modify: movie:casts: preferences).
  2. when memory fills up and new media is required the last used assets are unloaded before the new media is attempted to be preloaded.
  3. any Shockwave compressed sound file must be decompressed into memory to its normal size before playing.

There is also another factor in Director that makes the following statement get mentioned often. "But it worked fine in authoring!" It's called the "use temporary memory" check box in general preferences. Use Temporary memory is great when creating the title but when testing it, turn it off. Otherwise, you will not be testing in a realistic environment and will have access to more memory than the finished title can use.

Now to get down to creating a memory management strategy. If you can break your title down into loadable groups, it is important to know how much memory you've got to play with. For testing memory consumption in a CD, you can use either my shareware memory monitor, the commercial one off the Director Developer Toolkit or Michael Scaramozzino's Ramlight.

Shareware memmon:
http://www.mcli.dist.maricopa.edu/director/demo.html and search for the keyword memmon

Director Toolkit:
http://www.blacktop.com/zav/toolkit

Ramlight:
http://www.dreamlight.com/webshop/software/ramlight.htm

For Shockwave, you can use my Shockmon at http://www.blacktop.com/zav/shockmon

Now if you have a small title, and everything will fit in memory, just set your internal cast (or your movie if you're using dir 5) to preload before or after frame 1. This will stuff as many of the cast members that are set to "load when needed" to come into memory. There, for a simple (aka small) movie you're done! If your movie is a bit larger and has several sections, it is a good idea to have several approaches to your memory management. Realize that it takes time to load media and the time that you will expect off of a CD ROM is much more than that off a hard drive. So let's pose several options:

  1. load the media for the next frame or frame set in the current frame while the user is busy.
  2. when loading a large section, play a sound loop and animation till the media is loaded.
  3. when loading a very large section break it up into pieces that can be loaded while the user is busy.
  4. remember that Director may prefer if you unload what you don't need to guarantee free memory.

The only reason that I mention #4 is that Director has the freebytes and the freeblock. The freeblock is the largest free chunk available to load media into. Often the freebytes is huge but fragmented and the freeblock is the actual value that you care about for loading large pieces of media. Remember if you control when director loads and unloads media rather than relying on Director's internal scheme itself, you get a bit of an upper hand getting your title to ship.

The commands you care about:

preload:
with no arguments, will attempt to preload the entire movie into ram.
preload startFrame, endFrame:
will attempt to load all score media in the designated frames.
preloadMember startMember, endMember:
will attempt to load members from the start member to the end into ram.
unload:
with no arguments, will attempt to unload the entire movie from ram. Except media displayed on the current stage frame.
unload startFrame, endFrame:
will attempt to unload all score media in the designated frames. Except media displayed on the current stage frame.
unloadMember startMember, endMember:
will attempt to unload members from the start member to the end into ram. Except media displayed on the current stage frame.
the memorySize:
the total memory for Director, a Shockwave or Projector in bytes.
the freeblock:
the total largest free chunk of memory available in bytes.
the freebytes:
the total amount of memory available in bytes.

So to create management routines, take all the above points together and determine what will work best for your title. When you determine your approach, create generic management routines that you can call with an argument or a list to either load or unload media. At certain points in my titles, I simply check the freeblock and see if any memory needs to be unloaded on an emergency. If that's the case, I simply unload everything that is not needed in the current section. All this is accomplished through routines explained in this paragraph. Greater control of what is needed for each section is accomplished by neatly organized casts and several lists of required members for each section. Since preloadMember takes a starting and ending member, these lists only contain the start and end members needed and all the required members will load. If your title uses reusable members throughout the title, you can set them to never unload or you can have a separate list of reusable items to load with the previous list.

Now while you've got all that working, run one of the previously mentioned memory monitoring tools to see that it is actually working. For a final test, put your movies on a CD with a special check in the projector's startup code that will open one of the memory monitoring tools if a special key is held down on startup. Then you can see your memory management running in the target environment and rest assured that your clients will not have memory problems when running their title.

A Director user since 1987, Alex (Zav) Zavatone has worked on the engineering teams for both Director and Shockwave, 4, 5, 6 and 7. Recent investigations find him developing foundation classes for Director with asynchronous process management and other life threatening activities. In its early days, he had something to do with this Internet thing known as "DOUG". A noted ne'erdowell, slacker and laggard, Mr. Zavatone is nonetheless is trusted by children, small animals and the elderly. In his spare time, Mr. Zavatone rehabilitates lame desert trout.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.