Articles Archive
Articles Search
Director Wiki
 

Ending CPU Hogging in Director

December 1, 2003
by Alex Zavatone

Apparently since the dawn of time, Director has had quite an affinity for the processor and its cycles. In fact, Director appears to like the processor so much that when when running, there is little to no free processor time. In the old days, with Mac OS 9 and before, Director on the Mac had the CPUHogticks to determine an amount of time to give back to the processor but nowadays, with OS X, that command does not exist and CPU hogging becomes an issue because of the following reasons:

On laptops, hogging the processor will result in 100% processor allocation which will result in a hot processor which will turn the fan and will lessen battery life.

On OS X, your whole system becomes less snappy.

Director does not play well with other applications if it asks for all the remaining processor power AND may take processor power away from other subsystems that the projector may access. Hogging the processor makes distributing Director utility applications unrealistic.

So, enough about how much this sucks, why does it happen and how do we fix it?

From several conversations last century with various Director engineers, I learned several things. Among them, the movie level on idle handler is only called when Director has nothing else to do or has spare cycles. So letting this register, this means that at times Director has spare cycles, cycles that could be used by any other application. Also, if Idle gets called and Idle completes and there are spare cycles left, Idle will get called again and again till there are no spare cycles present thereby preventing the spare cycles from being used by other processes. This will happen whether you have put an on idle handler in your movie or not. Following this logic, it would seem that if we were able to trap this idle event in an on idle handler and sleep Director for an appropriately small period, then Director would not be gobbling cycles and since it wasn't using them anyway, this would free up the processor for other tasks and still allow Director to operate as fast as it needs to!

Now I know you're saying "oh, that sounds lovely Sparky, how do you suggest we do this?" And this gentle reader, is the good part.

From a conversation with Warren Ockrassa about his love of undocumented Xtras, I started playing around with one of those Xtras. In it, I found a "sleep" command. Amply caffeinated, an AHA moment happened and I decided to see what would happen if Director slept for 1 millisecond when idle was called.

That was it.

This simple handler stopped Director from hogging the processor.

on idle
  sleep 1
end

If you run or produce Director software for OS X, this has profound effects, all of them good. Director running on Windows also exhibits better processor usage but the benefits are less pronounced.

The trick is that whether running a Director movie on the Stage or in a MIAW, there is only a limited amount of idle or free time that Director can use an opportunity to call on idle. This means that only one of these on idle handlers is needed for any Director session. More than one on idle handler split between the stage and any running MIAWs is not needed.

Now, the tricky part is that this afore unmentioned Xtra which comes from Macromedia has different names in different versions and platforms of Director. Great.

It started out as the "UI Helper Xtra" made to support Director's Export to Java functionality in D7. Now in Director MX, it has been renamed the "Watcher Helper Xtra". On Windows, it was originally called "JavaUIHelper.x32". On certain versions of Director for windows (8, 8.5), this xtra must be installed off of the goodies or Xtra partners folder on the Director install CD. As far as I know, it is called "WatcherHelper.x32" on DMX for Windows.

In any case, you can always go to the message window and type the following line to see if the xtra you suspect has the sleep method in it.

put interface(xtra "the name of the xtra I suspect is the one I care about")

Once you have identified the xtra for the version of Director you are using, you must manually include the xtra in the movie xtras list for use within a projector since it is not an asset Xtra.

Now make your projector, launch it and run a processor monitoring app. You'll be pleasantly surprised. A snapshot of a DMX projector playing nice with other apps on my 1G Ti PB is listed below.

A projector using only 11% of the CPU is a joyous site to see.

Think we're done yet?

There are caveats though and luckily solutions. Colin Holgate mentioned that this technique is great unless your projector is set to "not animate in the background". Sure enough, when I tested it, a no-animate projector on OS X started eating up all the processor power when put into the background. Colin's solution to this was to use a regular "animate in background" projector and to trap the on activateApplication and on deactivateApplication handlers. Within those handlers, set a global used for the sleep value to 1 on activateApplication and to a higher number on deactivateApplication. This technique combined with a regular animate-in-background projector allows the projector free up even more time when in the background.

Requested since before the dawn of time, the end to cpu hogging in authoring and Projectors is here.

Thanks for contributing to this discovery/technique go to:
Warren Ockrassa - General troublemaking & research.
Joe Siponen - pointing out the xtra name differences between versions
Troy Rollins - MIAW experiments
Colin Holgate - By default
The rest of Hopper-ex and the Direct-L mailing lists who helped flesh out this technique.

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.