3

I am writing a program which logs my browsing history in a text file. It grabs url from chrome window and writes it to text file. It gets chrome window handle as a parameter and writes url in out parameter. The code looks like this:

static AutomationElement elm;// = AutomationElement.FromHandle(handle);
static AutomationElement elmUrlBar;  
public static void GetChromeUrl(IntPtr handle, out string url)
    {
        string namedProperty = "Address and search bar" ;
        url = null;
        elm = AutomationElement.FromHandle(handle);
        elmUrlBar = elm.FindFirst(TreeScope.Descendants,
          new PropertyCondition(AutomationElement.NameProperty, namedProperty));
        if (elmUrlBar != null)
        {
            AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
            if (patterns.Length > 0)
            {
                ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
                url = val.Current.Value;
            }
        }
    }

I call this method using timer callback in every say 5 seconds and it returns me correct url from chrome browser window. So it does it's work well although it seems that it doesn't free up memory occupied by AutomationElement object and RAM used by this application is constantly growing. I profiled it using dotMemory 4.0 and ants memory profiler 8 and it shows that automationElement objecrs are created but never deleted by garbage collector. Does anybody know how to resolve this issue?

  • what happens if you were to refactor the code and wrap the AutomationElement Object around a `using(){}` statement? could you also perhaps Implement `IDisposible` ? could you also call `GC.Collect()` – MethodMan Sep 02 '14 at 15:31

1 Answers1

1

The company I currently worked at ran into this problem as well.

This article has the answer to the problem in it UIAutomation Memory Issue

You basically need away to call GC from the application you are automating because automation elements will get added to the large object heap and take 3~ minutes to be disposed of if no pointers exist to them at that point.

Community
  • 1
  • 1
Max Young
  • 1,522
  • 1
  • 16
  • 42
  • Facing a similar issue. Can you please share your solution code if possible? – Awais Umar May 20 '20 at 02:51
  • @AwaisUmar Unfortunately, it is not really feasible to do what I was doing to test. Calling `GC.Collect()` yourself is not a good idea and you would need to be able to expose some method of communicating from the test application to the AUT (application under test) to trigger it. All of this would be very problematic. After running into more memory leaks that were not resolvable even using the test method we finally built into our test application the ability to monitor the AUT memory consumption and restart it when it gets to a certain memory limit. – Max Young May 20 '20 at 14:49
  • @AwaisUmar The specific bug we ran into later was that WPF itself is leaking AutomationElements as per this forum post https://www.infragistics.com/community/forums/f/ultimate-ui-for-wpf/77267/dockmanager---memoryleak#data-id=436587 about Infragistics controls. This issue is not limited to infragistics, it is related to any control using the ItemsControlAutomationPeer. If you look for a post from Brian Lagunas from 6 years ago you will see the issue I am referring too. – Max Young May 20 '20 at 14:53
  • @AwaisUmar You can see the bug Brian refers to in this file https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs,200172deda1fceba and it is still present to this day. Essentially they are using weak references but they use a strong reference to the item as they key for the weak reference so it never gets cleaned up. This will happen for controls like TreeViews and ListViews for example but I am sure this is applicable to more than just those controls. – Max Young May 20 '20 at 14:55
  • @AwaisUmar If you ask a question about this I will move all this to the question for better formatting but I dont think it is appropriate to reference all this here. – Max Young May 20 '20 at 14:55
  • Thank you for responding back Max. I am actually using the certain memory limit approach you have mentioned in your first comment. I though it would have been better to let the GC do its work on its own. I have a question open related to the issue https://stackoverflow.com/questions/61876201/automation-in-windows-form-application-eating-a-lot-of-ram – Awais Umar May 21 '20 at 00:55