Memory leak in MapView

4939
4
Jump to solution
09-02-2015 03:45 AM
TomášVepřek
New Contributor

Hi,

I have WPF application with ArcGIS SDK for.NET version 10.2.6, when I add multiple MapView controls into some container, lets say StackPanel, each map with its own ArcGISTiledMapServiceLayer, and then i remove them, memory they've used never gets collected. After adding more MapViews application crashes even though memory from removed ones should be released.

If I run the app and add MapViews without layers in them, this problem doesn't occur, because when I remove them from their container memory gets freed.

I attached sample application that simulates this behavior. Just add few maps, then remove them, repeat this step few times and you'll eventually see app crash.

Thank you

1 Solution

Accepted Solutions
AnttiKajanus1
Occasional Contributor III

It looks like GarbageCollection isn't occurring in the samples. If you want to release the memory you need force collection. Easy way to test this is to add new button with following code. Add maps, remove then and collect memory.

   GC.Collect();
   GC.WaitForPendingFinalizers();

Here is memory usage using this approach.

View solution in original post

4 Replies
AnttiKajanus1
Occasional Contributor III

It looks like GarbageCollection isn't occurring in the samples. If you want to release the memory you need force collection. Easy way to test this is to add new button with following code. Add maps, remove then and collect memory.

   GC.Collect();
   GC.WaitForPendingFinalizers();

Here is memory usage using this approach.

TomášVepřek
New Contributor

Thanks for the answer, but forcing GC (Garbage collector) isn't good practice and how should we as developers know that map with specific layer needs manual call of GC?

When I think about it, it looks like there are probably objects that are moved to GC's Large Object Heap and thus they get collected after a long time (if ever) because they live in second generation of GC.

When you know, that using layers such as ArcGISTiledMapServiceLayer creates those objects (many smaller ones or few big ones?), shouldn't  the SDK, instead of those who use it, take special care of those layers when map is unloaded?

I think it could be considered as bug, because when unloading and loading few maps with several layers, you quickly run out of memory and this is something no one would expect, because everyone hopes GC takes care of controls that are no longer loaded.


Thank you.

dotMorten_esri
Esri Notable Contributor

The .NET Garbage Collector currently has problems with realizing it needs to collect when a lot of native code uses memory. You can instead of calling GC.Collect, add some memory pressure to make the GC slightly more aggressive and do the work it needs to. Unfortunately this is a global settings and therefore not something the SDK should do because it needs to play well with other SDKs and apps.

https://msdn.microsoft.com/en-us/library/system.gc.addmemorypressure(v=vs.110).aspx

Try and set it to for instance 500mb and you should usually see GC get collected a little sooner without having to force GC.

NilankaD
New Contributor II

Encountered the same memory leak problem in mapview. 

0 Kudos