Archived

This topic is now archived and is closed to further replies.

[java] sun.awt.windows.WGraphics.dispose()

This topic is 5871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When profiling my game, I found that a large amount of CPU time is used up by this function; sun.awt.windows.WGraphics.dispose() Anyone know what it''s used for and why is called? I couldn''t find any information around the web. / dr-m

Share this post


Link to post
Share on other sites
That function is the "destructor" method of the Windows specific graphics context object. The carbage collector calls the finalize() method of each object it is about to remove from memory. I''ve myself ran into this very same problem and the only solution is to not create graphics objects when not necessary.

One source of unnecessary objects is calling getGraphics() method in every loop of your game (or other program). Under some runtimes (e.g. JDK 1.3.1) it creates a new Graphics object each time it is called. So you should try to get the Graphics object once and then use it until something bad happens (e.g. an exception is thrown when you try to use the Graphics object). Only then you should get the new Graphics object.

As an example:

public class MyFrame extends Frame
{
Graphics myGrfx;
...
public void init()
{
myGrfx = getGraphics();
}

public void render()
{
try
{
myGrfx.drawLine( 0, 0, 10, 10 );
}
catch( Throwable anyError )
{
myGrfx = getGraphics();
}
}
}


Note that the performance hit from the try-catch is almost nothing when no exceptions occur. Only if an exception occurs and the catch statement is executed it has a noticeable performance penalty. But in this case the exception is thrown very rarely if at all.

Share this post


Link to post
Share on other sites