question

jtorjo avatar image
0 Votes"
jtorjo asked ·

[uwp][c#] how to fully unload a user control's resources?

I have a quite complex app - a video editor.
At startup, I have a wizard that will allow the user to:

  • select the photos/videos

  • music to include in the final video.

When the wizard finishes, I show the main editor, and the user can press "Play".

For the 2 first windows (a. select photos/videos + b. music to include), I have them as UserControl classes. I place them in the top grid of the main window, and then I remove them, more or less:

     private async Task show_async_full_screen(Func window_creator, Func check_func, Func result_func) where W : Control, IDisposable {
         var w = window_creator();
         main_grid.Children.Add(w);
         await Task.Run(() => {
             .... wait for control to end
         });
         var result = result_func(w);

         main_grid.Children.Remove(w);
         w.Dispose();
         return result;
     }

The problem is this:
the two controls or some parts of them (the "select photos"/"select music"), still remain in memory, eating resources and eating CPU.

This is really bad, since it makes my app sluggish (every now and then, freezing for about 100ms or so).

I know the issue is the above 2 controls, since if i remove them (and preselect photos/music - for testing), the freezing does not occur.

How can I fully unload a control -- so that it releases all its resources and does not eat any CPU?

windows-uwp
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When you have removed these two controls from the parent control, this issue should not happen, maybe there is still some connection between them and the main window. Do you subscribe the event handlers for two controls inside the main window but not unsubscribe? Or is there any background operation in the two controls?

1 Vote 1 · ·

After some hard core debugging + profiling, I found the culprit, please see below.

0 Votes 0 · ·

1 Answer

jtorjo avatar image
0 Votes"
jtorjo answered ·

After some hard core debugging + profiling, I found the culprit: the garbage collector

Long story short, I have optimized everything so that the controls are properly unloaded now. But it is the garbage collector that runs every 2-3 seconds, and takes about 50-80ms. That's huuge in my case - so I'll need to find a way to minimize that.

Not looking forward to it, but a man's gotta do what a man's gotta do.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.