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

John Torjo 861 Reputation points
2019-12-09T11:59:04.797+00:00

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?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. John Torjo 861 Reputation points
    2019-12-12T18:55:49.727+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.