I'm developping a UWP application with the AudioGraph API.
As garbage collection produces clicks in the audio output, memory mamagement is very important in this application. I use many memory optimization techniques so that code allocates as few memory as possible.
Some user controls in my application uses the composition API to render some simple graphics.
Should we dispose composition objects when these user controls are unloaded ? Or is it overkill ?
I wasn't able to measure a real difference using the memory profiler before and after implementing the dispose pattern...
Here is some sample code.
Member variables :
private bool _disposed;
private readonly ContainerVisual _containerVisual;
private readonly SpriteVisual _backgroundVisual;
In the user control's constructor :
Compositor compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
_containerVisual = compositor.CreateContainerVisual();
_backgroundVisual = compositor.CreateSpriteVisual();
_containerVisual.Children.InsertAtTop(_backgroundVisual);
There is also a "Layout" method in the user control that changes brushes, size, offset, etc.
And in the Unloaded event of the user control :
if (!_disposed)
{
_containerVisual.Dispose();
_backgroundVisual.Dispose();
_disposed = true;
}
@nseveno you should not have to manually call these methods. When the control goes out of scope, the associated resources should get disposed. If you want to control when it gets disposed, you can use the 'using' statement. You can find more details here
@ranjeshj I will then call Dispose to release the resources as early as possible. Thanks...
Most helpful comment
@nseveno you should not have to manually call these methods. When the control goes out of scope, the associated resources should get disposed. If you want to control when it gets disposed, you can use the 'using' statement. You can find more details here