0

I'm writing a game in C# with SharpDX... So, for drawing a frame, currently i'm using a Timer, with interval set to 1; But this is too slow for a game, so i want something faster... First of all, if I use another thread for the game (and another for the Window itself) and i use a "[while (InGame)]" statement, it is fast, but the GC will never release a single object, and the memory usage just going up and up...

IF I use the SharpDX built in "RenderLoop" function, it is fast also, but the GC still doesn't do anything...

I've tried to override the Window form "WndProc" function, but the game only refresh itself then i'm moving the mouse like a crazy men...

This is my first real game, with a lot of functions, so if any of you have any idea, how would i fix it, or what should I use, I would appreciate it... There must be something in the Window Form lifecycle like a

void FormLife
{
     WndProc();
     HandleEvents();
     etc...
}

(It was just a hard example...)

OR how can i force the GC to do its job, at the end of the while(InGame) state ? I've tried GC.Collect(); but its dont work...

Sam
  • 7,252
  • 16
  • 46
  • 65
Rabir
  • 59
  • 1
  • 1
    [You can force the garbage collector to do its job](http://stackoverflow.com/questions/233596/best-practice-for-forcing-garbage-collection-in-c-sharp) – TheNorthWes Jun 20 '14 at 21:44
  • If your objects aren't being collected, they're not garbage. Make sure you don't keep references around to objects you don't need anymore. – Gabe Jun 20 '14 at 21:49
  • 1
    If GC.Collect is not working you must still have references to some objects – Chad Dienhart Jun 20 '14 at 21:49
  • 3
    @AdmiralAdama You can, but you shouldn't. – Danny Varod Jun 20 '14 at 21:50
  • _a Timer, with interval set to 1_ What does that mean? 1ms? Anything under 25ms is wasted anyway as the resolution is around 25ms. – TaW Jun 20 '14 at 21:51
  • I know @DannyVarod, I was praying reading those posts would lead to that conclusion and not result in a quick answer on how to do it and calling it. – TheNorthWes Jun 20 '14 at 21:53
  • @TaW Depends on the type of the timer and the efficiency of the running code. – Danny Varod Jun 20 '14 at 21:53
  • @AdmiralAdama I would have thought that forcing the GC to collect would seriously impede performance (depending on how often it's being called of course)? – Sam Jun 20 '14 at 21:54
  • It does @Sam I highly doubt that would ever be desirable. – TheNorthWes Jun 20 '14 at 21:57
  • @chaddienhart I ran a test in a different project: while (true) { VI.Draw(DirectX.renderTarget, DirectX.d2dFactory, new Vector2(300, 300), Angle, 1); GC.Collect(); } When it exit the VI.Draw, the references should be erased, as C# works... But GC.Collect still doesnt remove them... – Rabir Jun 21 '14 at 09:57
  • Also, when i run the Game UI in the Main UI calss, the GC collect everything, but when i run the game UI in different class, even with "GC.Collect" GC dont do anything O.o – Rabir Jun 21 '14 at 10:05
  • You didn't post much code so it is hard to say where you are holding a reference. If you have subscribed to any events you have to make sure you unsubscribe to release the object (A common case of holding references). In the past I've used windbg to find the root cause of these types of memory issue. VS2013 has a [memory profiler](http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/20/using-visual-studio-2013-to-diagnose-net-memory-issues-in-production.aspx) built in – Chad Dienhart Jun 21 '14 at 17:07

4 Answers4

5
  1. Don't put your game logic in the UI classes.

  2. Use a multimedia timer and a dispatcher to return to main thread for rendering. From my experience a cycle of 5mSec works well. (Lower will waste CPU time).

  3. Use dispatcher priority of Input so that the timer won't choke the UI and prevent it handling user events.

  4. On timer event, if the previous update hasn't completed, return immediately without doing anything (you can use System.Threading.Interlocked.CompareAndExchange as a cheap sync method).

  5. To make game run faster, try to reuse objects and change their states or to pass short living immutables so that the GC won't delay your program.

  6. Use the IDisposable pattern where applicable.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
1

If GC.Collect does not free memory you have a memory leak. Use the memory profiling tool of your choice to spot the leak (PerfView, Windbg, .NET Memory Profiler, YourKit, ....)

Besides this the best allocation is no allocation at all. If you reuse objects and keep unused objects in a pool you can get rid of GCs almost entirely. Even if you stay away from Gen 2 collections which can block your game for hundreds of ms you also need to keep a sharp eye on Gen 0/1 collections which can also cause noticeable delays.

See http://social.msdn.microsoft.com/Forums/vstudio/en-US/74631e98-fd8b-4098-8306-8d1031e912a4/gc-still-pauses-whole-app-under-sustainedlowlatency-latencymode-despite-it-consumes-025gb-and?forum=clr

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
0

You don't typically have to worry about memory management. If you are making sure not to hold on to references then the garbage collector should do its thing automatically based on memory pressure.

You can force it yourself using GC.Collect but you need to be absolutely certain you really need to. Most of the time its not required. Check this thread for more info and points to consider: Best Practice for Forcing Garbage Collection in C#

Community
  • 1
  • 1
Venr
  • 937
  • 7
  • 10
0

Since the objects are not being collected you must be holding a reference to them somewhere.

You didn't post much code so it is hard to say where you are holding a reference.

In the past I've used windbg to find the root cause of of memory issues. VS2013 has a memory profiler built in (might be easier to use).

A common case I've seen of holding references is via event subscription. If you have subscribed to any events you have to make sure you unsubscribe to release the object. see Why and How to avoid Event Handler memory leaks? for more details. That doesn't look like your problem.

Community
  • 1
  • 1
Chad Dienhart
  • 5,024
  • 3
  • 23
  • 30
  • I've fixed the problem :D Now i only need to potimize my game, above 20 ship it is laggink so much O.o – Rabir Jun 22 '14 at 12:13
  • So what was the problem in the end? Did you use the memory profiling tools to resolve it or just figure it out on your own? you should look at using the other profiling tools to help optimize your game: _____ http://msdn.microsoft.com/en-us/library/z9z62c29.aspx ____ or this one:____ http://msdn.microsoft.com/en-us/library/ms182372.aspx – Chad Dienhart Jun 22 '14 at 16:12
  • I've fixed on my own... I like fixing things on my own since i learn a lot of new things while i work on them... – Rabir Jun 23 '14 at 18:16