releasing memory

Jul 20, 2010 at 8:18 AM

Hi radioman,

using your WPF demo i've found that clearing the list of GMapMarker won't release the memory occupied by these markers.

How can we release this memory?

Jul 20, 2010 at 8:25 AM

don't release, reuse objects

Jul 20, 2010 at 8:45 AM

I've tested the application and i saw this behavior:

i add some markers and they use some memory, then i use the clear all button and i see that some memory is released but not all (i think it'is because of the reus you are talking)

i add some markers again and they use memory again but the memory used by the application is more than before then with the clear button some meory i released .

after doing this sometimes i saw that the memory used the application was every time higher.

just to let you understand better at the start of the testing the application was using 37 MB after the testing it was using 200 MB without showing markers.

so how can i release the memory or how can i do a efficient reuse of these objects?

Jul 20, 2010 at 9:08 AM

just reuse them ;}

Jul 21, 2010 at 9:33 AM

i've tried to create a reuse of the image used in the marker's xaml but it gives me some problems

i'll post the code, this code is triggered by an event on a button, reads the number of markers the application has to show form a textbox and creates the markers

      private void Click_marker(object sender, RoutedEventArgs e)
          int Label_marker = int.Parse(Text_marker.Text, CultureInfo.InvariantCulture);
          RectLatLng Area = MainMap.CurrentViewArea;
          double AreaLeft = Area.Left;
          double AreaBottom = Area.Bottom;
          Random randlat = new Random();
          PointLatLng point;
          double lat = randlat.NextDouble();
          double lng;

          Image img = new Image();
          img.Source = new BitmapImage(new Uri("C:/RouteEventSosta.png"));
          img.Name = "icon";
          img.VerticalAlignment = System.Windows.VerticalAlignment.Center;
          img.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

          for (int n = 0; n < Label_marker; n++)
              lat = randlat.NextDouble();
              lat = lat * Area.HeightLat;
              lat = lat + AreaBottom;
              lng = randlat.NextDouble();
              lng = lng * Area.WidthLng;
              lng = lng + AreaLeft;
              point = new PointLatLng(lat, lng);
              GMapMarker m = new GMapMarker(point);
              UserControl elem = new EventSosta();
              elem.Content = img;
              m.Shape = elem;
              m.Offset = new System.Windows.Point(0, 0);
              m.ZIndex = int.MaxValue;

the exception is on  elem.Content = img  and it says that the element i'm working on has already a logical parent

what do you think about this code?   any idea to resolve the problem?

Jul 21, 2010 at 10:58 AM

..well to load the same image for each marker doesn't saves memory, try keep things simple, define image in user control and use it as the shape

Jul 21, 2010 at 11:29 AM

actually creating 10000 markers i saw that putting the definition of image in the usercontrol and using it as the shape, the 10000 markers use 120-130 MB, without the image they use 90-95 MB so i use the 25% of the memory less, so using only an image for all the usercontrol and not an image for each usercontrol saves memory.


Jul 21, 2010 at 11:46 AM
Hi to all, I've a similar problem: I place about 5000/6000 custom marker, and when I clear them the memory is not released (with a little image, 19x18 pxl, the memory allocated is about 70Mb). Radioman, what do you mean when you say "reuse"? Something about to create some GMapMarker, to mantain the reference to them in a vector and to flag them if are not yet used? And if I need a GMapMarker I've to search for the non-flagged GMapMarker? If I need to deallocate that memory (in my system 70Mb is a large amount), is a way to do it? I've created a simple user control with an image in it, and then I assign a new istance of user control in a new istance of GMapMarker. Where is the largest memory amount? In the image, user control or GMapMarker? Thanks for your work! Cosimo.
Jul 21, 2010 at 7:05 PM

..well in wpf, you can't actually 'release' memory, so you create as minimum as possible of visuals and keep reusing them.