Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6
This is the forum to discuss the Wrox book Professional XNA Game Programming: For Xbox 360 and Windows by Benjamin Nitschke; ISBN: 9780470126776
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 8th, 2007, 05:20 PM
Registered User
 
Join Date: May 2007
Location: , , Brazil.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dispose() methods

Hi,

I was looking at the racing game source code and almost all the classes implements the Dispose method. The implementation looks like this:
Code:
public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}
I'm not a C# expert but I was wondering the effect of this code... I guess it's to optimize the garbage collector job but I'm not sure how it works.

Also, this code is target for the Compact Framework ?? Since this one is somewhat "less optimized" ?

Best Regards,
Bruno Evangelista
www.brunoevangelista.com
"For what is a man profited, if he shall gain the whole world, and lose his own soul? or what shall a man give in exchange for his soul?" Matthew 16:26
__________________
Best Regards,
Bruno Evangelista
www.brunoevangelista.com
\"For what is a man profited, if he shall gain the whole world, and lose his own soul? or what shall a man give in exchange for his soul?\" Matthew 16:26
Reply With Quote
  #2 (permalink)  
Old May 12th, 2007, 06:46 PM
Wrox Author
 
Join Date: May 2007
Location: Hannover, , Germany.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to abi.exdream.com
Default

Well, there are 2 reasons for this code and before I tell you why I did it, keep the following rule in mind: Never dispose anything unless you really have to. For example it is a good idea to dispose FileStreams to close files, but for textures or your own game classes, disposing is usually not required!

The Racing Game is a really complex game for the compact framework and I guess the XNA team did not really expect someone to be that crazy and build a racing game in a few weeks with the early beta code ^^ There were many issues with the beta 1 and disposing hat a lot of issues, which were mostly fixed later. But I let the Dispose code stay in the Racing Game (I removed it from all other games as far as I can remember) because it still made sense to clear everything up after a game and prepare for a new level. Else the memory consumption would go as high as having all the data that is ever loaded in the game (all levels, all cars, all 3d models, etc.) all loaded up in memory at the same time. On the PC this is not really an issue because there are just 3 levels, but if you imagine a bigger game with more maps you probably want to make sure that you get rid of stuff you won't need anymore after completing a mission.

On the compact framework it was a very bad performance hit to have the Garbage Collector kick in from time to time. This was not just the textures and level data at the end of a mission, but all the foreach loops also created IEnumerator objects, which were disposed a few seconds later and made the game very jerky. I did not find out about this until later after I already added all the dispose code, but after removing all foreach loops and after making sure no objects are created in a frame, the game performance went up to a couple of hundred frames with all game logic on (with got less by adding more objects, more shaders and increasing to HDTV resolutions).

Anyway, the first line in the Dispose method makes sure everything is disposed that was created in this class and the second line (SuppressFinalize) just makes sure this Dispose method is not called again and the finalizer does not need to be executed, which also is good for the performance of the Garbage Collector because I usually Dispose stuff at the end of the mission or a game screen anyway.

Hope this makes sense ..

http://abi.exdream.com
Reply With Quote
  #3 (permalink)  
Old May 13th, 2007, 12:15 AM
Registered User
 
Join Date: May 2007
Location: , , Brazil.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Abi,

This post was very helpful to me!!!

I'm writting a boid simulation demo using XNA and I'm having a lot of problems with performance. The boid update algorithm is not optimized yet, so I need to check each boid with all others O(n^2).

And I couldn't update more than 100 boids in real time at 60 fps... Now that you mention about the foreach I realized that I'm using a lot of this loop in my code... O.o

Thanks a lot for the answer!


Best Regards,
Bruno Evangelista
www.brunoevangelista.com
"For what is a man profited, if he shall gain the whole world, and lose his own soul? or what shall a man give in exchange for his soul?" Matthew 16:26
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dispose Pattern guntank C# 0 September 26th, 2006 11:08 PM
Dispose() pzmrcd .NET Framework 1.x 2 March 2nd, 2006 03:19 PM
Question : Dispose mike_abc Pro VB.NET 2002/2003 1 May 14th, 2005 03:17 PM
dispose of OWC 9.0 object Ramasubbu Forum and Wrox.com Feedback 1 April 22nd, 2004 09:23 AM
dispose Ibn_Aziz C# 3 February 26th, 2004 05:17 AM



All times are GMT -4. The time now is 04:59 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.