Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElementHost size causes slow wpf open/load with high memory usage

The Problem:

We have a Winforms application, which can open up another WPF Window. This window contains various icons and text and isnt overly complex. Back to the main window, the winforms one, this also has a WPF control embedded using ElementHost(ElementHost is specifically designed to make it possible for Winforms to host a WPF Control). We have noticed however, that once the memory usage of this application get quite high, this second WPF Window can take a considerable amount of time to open.

We have made a small test application in VS2012 which is able to replicate the problem. The test application opens a Winforms window which has two buttons; "Add 4gb memory usage" and "Open WPF Window". The WPF Window has a single small icon on it(48*48) and that is all. When we add the 4Gb memory usage and attempt to open the window for the first time, it takes around 7 seconds to open the window.

CODE: I have made the test app and source code publicly available through BitBucket.

Things we have found out:

  • The app is definitely running in 64-bit, as it will throw OutOfMemoryExceptions in 32-bit.
  • Without the memory usage the window opens immediately.
  • With the memory usage, the window is only slow the first time it is opened.
  • If the WPF window doesnt have an image, it opens immediately.
  • Reducing the size of the elementhost which is on the winforms control, to say 1*1, the window opens immediately. Obviously this isn't a great solution as it doesnt show much of our WPF Control at this size.
  • The ElementHost size in our application is about 1000*700, but we noticed there is a big drop off point when reducing the ElementHost size, so it will take this 10 seconds, until it gets to around 350*350, at which point it suddenly improves to be fast again. This could be computer dependent, or maybe a limitation of some kind.
  • Removing the image from the wpf window will make it fast again.
  • Adding new images as resources, and on the WPF window, linearly slows the open time (e.g. 1 image = 7 seconds, 2 images = 14 seconds).
  • Experimented with making sure the image is the same size it is being displayed at so there should be no resizing, but this didnt seem to make any difference.
  • Experimented with different image types, .png, .tiff, .ico but they all seemed to be slow.

Has anyone else experienced this problem / Any suggestions on what our next step could be?

like image 296
user3483066 Avatar asked Apr 01 '14 00:04

user3483066


1 Answers

After some more analysis using a profiler, this article seemed to be the same issue as what I was experiencing.

typeof(BitmapImage).Assembly.GetType("MS.Internal.MemoryPressure")
.GetField("_totalMemory", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, Int64.MinValue / 2); 

Not the nicest solution, but it definitely fixed the problem.

like image 147
user3483066 Avatar answered Nov 08 '22 08:11

user3483066