Is there a good way to dereference WPF / C # objects for garbage collection?

Application background

Our platform is a one-time WPF application. We have a “shell” that contains the navigation menu structure, and it hosts our own “page” classes. When you go to a new page, we replace the contents of the shell (essentially).

Problem

So, I work for a company that is working on an extremely large software project. We have a lot of code that we encountered with memory problems.

The problem is that in our application there are many places when events are connected and are never freed. I’m not sure why the developers do this, I assume that they expected the objects to be cleaned every time the user goes to a new "page".

We do not have the ability to refactor each page (in this version). Is there a way with C # to remove all links from an object? (Therefore, so that the garbage collector throws this object along with all its internal links)

We are trying to reclaim this memory, but it’s rather difficult to find objects that still link to our pages (object links) when we have WPF.

We looked at visual and logical trees and used profiling applications to help us manually clean things up (to prove the idea), and it turned out to be extremely complicated.

I leaned back and thought: why are we all doing this to find links to objects, can't we just “dereference” this “page” when it is closed?

What brought me here :)
Any help is much appreciated!


UPDATE 1

The comments asked the following:

Q: Is the application in use. really memory problems? How do they manifest / show up? Or is that memory hanging around until GC2 happens? - Mitch Wheat

A: He has memory problems. If you leave the page (a property that contains the page for the new object), the old object will never be collected by the garbage collector. So the memory just continues to grow. If our company has started working with this application. The first thing we should pay attention to is the introduction of WeakEvent templates and the use of more routed commands in WPF.

UPDATE 2

I was able to come up with my own solution .

+4
source share
3 answers

I decided WeakEvents to solve this problem.

Unfortunately, Microsoft recommends using your WeakEventManager as a base class and creating a manager type for EVERY event in your application !!! I tried to write a manager who inherits this base and gets it more "generic" (not in the C # term for general). This "general" event manager was not possible with the Microsoft base class. I had to write an event manager from scratch.

Thanks for any help posted on this question.

+1
source

Have you used CLRProfiler? I find it pretty good in finding objects, as well as finding what contains links to them.

This How To Page ...

http://msdn.microsoft.com/en-us/library/ms979205.aspx

... links to the download site.

However, I think the answer to your question is no. There is no way to say that “the foo object is no longer required,” pick it up, regardless of whether it is rooted or not. ”Collecting the root object may cause any harm to your application.

+1
source

If a page with holes goes out of scope, including all the underlying data, GC will collect it. Eventually. However, if this page is referenced by everything that is still in scope, the page and all its data will remain in memory, and GC will not be able to collect it.

As far as I know, there is no simple button when it comes to fixing poor memory management. This means that the only thing, in my opinion, is to do the job of cleaning the links. This will simplify the GC for collecting objects that go beyond.

You can suggest that GC go through its algorithm to see if it is possible to build something by calling GC.Collect (). However, this will do very little, except for processor cycles, if the data is really not available.

The question is for Phobis, how much memory does your application use?

EDIT:

Link to CLR Profiler 2.0, which should work for .net3.5 applications. http://www.microsoft.com/downloads/details.aspx?familyid=a362781c-3870-43be-8926-862b40aa0cd0&displaylang=en

-1
source

All Articles