dwmkerr / dwmkerr/sharpgl

Improve WPF Garbage Collection Performance

Open
#40 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C#
Stars
804
Forks
294
Avg merge
3d 23h
Merged PRs (30d)
1

Description

If you're using SharpGL in a WPF application, and you're using RenderContextType.FBO, then when the OpenGLControl responds to its DispatchTimer's Tick event (see the timer_Tick method in OpenGLControl.xaml.cs under the OpenGL.WPF folder) it will call the static HBitmapToBitmapSource(IntPtr) method (see BitmapConversion.cs). To prevent possible memory problems when too much is going and and garbage collection isn't fast enough, the HBitmapToBitmapSource method includes a call to GC.Collect() in a finally block that occurs with every call.

I have found this to cause too much of a slow-down with fast changing scenes, and to speed things up, I've made the following changes: I added a "static int gcCount = 0;" and a "static readonly int MaxCyclesBeforeGC = 50;" to the BitmapConversion class. Then, in the HBitmapToBitmapSource method's finally block have the following instead of the GC.Collect() call:

```
gcCount++;
if (gcCount > MaxCyclesBeforeGC)
{
gcCount = 0;
GC.Collect();
}
```

You can play around with the value for MaxCyclesBeforeGC, but NOT calling GC.Collect() with every cycle makes for a much smoother result and performing the garbage collection every so often seems to keep any memory problems in check.

Just a friendly suggestion.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.