Coder Social home page Coder Social logo

Memory Leak about mapsui HOT 10 OPEN

xiaoliangde avatar xiaoliangde commented on July 4, 2024
Memory Leak

from mapsui.

Comments (10)

pauldendulk avatar pauldendulk commented on July 4, 2024

What does the code do? Is this your solution? Or is this to demonstrate the memory leak? How does it show that there is a memory leak?

from mapsui.

xiaoliangde avatar xiaoliangde commented on July 4, 2024

What does the code do? Is this your solution? Or is this to demonstrate the memory leak? How does it show that there is a memory leak?

—— can't be execute destructor function(like annotation). But if others or this one not contain the mapControl that can execute desctructor. so now i create mapControl on "OnAttachedToVisualTree" to reduce the problem.

from mapsui.

xiaoliangde avatar xiaoliangde commented on July 4, 2024

What does the code do? Is this your solution? Or is this to demonstrate the memory leak? How does it show that there is a memory leak?

to simple test the problem like this:

class MapControlEx: MapControl
{
    public MapControlEx()
    {
        var tileLayer = Mapsui.Tiling.OpenStreetMap.CreateTileLayer();
        Map?.Layers.Add(tileLayer);
    }

    ~MapControlEx()
    {
        Dispose();
        Trace.WriteLine(GetType().FullName);
    }
}

class ControlEx : ContentControl
{
    ~ControlEx()
    {
        Trace.WriteLine(GetType().FullName);
    }
}

image
image

wo can see the MapControl can't execute the destructor. maybe the control member reference some global variant to cause the problem.
Is there any remedy available on this version to this issue?

from mapsui.

xiaoliangde avatar xiaoliangde commented on July 4, 2024

What does the code do? Is this your solution? Or is this to demonstrate the memory leak? How does it show that there is a memory leak?

guess other :

class MapControlEx : MapControl
{
    public MapControlEx()
    {
        var tileLayer = Mapsui.Tiling.OpenStreetMap.CreateTileLayer();
        Map?.Layers.Add(tileLayer);
    }

    ~MapControlEx()
    {
        Trace.WriteLine(GetType().FullName);
    }
}

class ControlEx : ContentControl
{
    ~ControlEx()
    {
        Trace.WriteLine(GetType().FullName);
    }
}

image
image

can't execute the "~MapControlEx()" too.

from mapsui.

pauldendulk avatar pauldendulk commented on July 4, 2024

I created a memory leak test like we had for Xamarin.Forms and it is green #2678. I have to investigate your kind of testing next.

from mapsui.

pauldendulk avatar pauldendulk commented on July 4, 2024

I looked into this a bit more. Not completely sure what exactly the problem is your struggling with. My guess is that you do not see an increase in memory use but you want to the finalizer to be called. The MapControl implements IDispose however and you should call Dispose() on it. In the MapControl.Dispose method we call GC.SuppressFinalize(this). So, I you want to cleanup something override the MapControl.Dispose().

Also I wonder if it is a good idea to inherit from the MapControl. Nowadays I am more inclined to make classes sealed because it is hard to correctly design for inheritance, and it is hard to implement a derived class because you have to guess how the parent class works.

from mapsui.

xiaoliangde avatar xiaoliangde commented on July 4, 2024

I looked into this a bit more. Not completely sure what exactly the problem is your struggling with. My guess is that you do not see an increase in memory use but you want to the finalizer to be called. The MapControl implements IDispose however and you should call Dispose() on it. In the MapControl.Dispose method we call GC.SuppressFinalize(this). So, I you want to cleanup something override the MapControl.Dispose().

Also I wonder if it is a good idea to inherit from the MapControl. Nowadays I am more inclined to make classes sealed because it is hard to correctly design for inheritance, and it is hard to implement a derived class because you have to guess how the parent class works.

Pay attention to the test in the last screenshot. The use of inheritance here is only to simplify the proof that it has not been released by GC. During the usage process (starting from the constructor), I aimed to detect the release of static and dynamic resource management, and then discovered that the view tree could not be cleaned up. Then we investigated various controls and found that there was an issue with locating the map control. As it is an editor (with a history it maybe reuse), it has to be written to the "AttachViusalTree" event and set to null during “DettachVisualTree” so that the parent control can be released and the impact range can be reduced. I will continue to observe the impact of this issue in the future. Thank you for your reply.

from mapsui.

pauldendulk avatar pauldendulk commented on July 4, 2024

If you can provide a minimal reproducible sample I will further investigate this.

from mapsui.

xiaoliangde avatar xiaoliangde commented on July 4, 2024

If you can provide a minimal reproducible sample I will further investigate this.

public partial class MainWindow : Window
{
    class MapControlEx:MapControl
    {
        ~MapControlEx()
        {
            Console.WriteLine($"------  Destruct:{GetType().Name}");
        }
    }
    class ControlEx : ContentControl
    {
        ~ControlEx()
        {
            Console.WriteLine($"------  Destruct:{GetType().Name}");
        }
    }

    class ControlOnDisposeWithMap00:ContentControl,IDisposable
    {
        private MapControl _cMapControl;
        public ControlOnDisposeWithMap00()
        {
            Content = _cMapControl =  new MapControl();
        }

        ~ControlOnDisposeWithMap00() => Console.WriteLine($"------  Destruct:{GetType().Name}");

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                _cMapControl.Dispose();
                Content = _cMapControl = null;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
    class ControlWithMapDisposeOnDetach01 : ContentControl
    {
        private MapControl _cMapControl;
        public ControlWithMapDisposeOnDetach01()
        {
            Content = _cMapControl = new MapControl();
        }

        protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
        {
            base.OnDetachedFromVisualTree(e);
            _cMapControl?.Dispose();//new idea to test... it run to Destructor
        }

        ~ControlWithMapDisposeOnDetach01() => Console.WriteLine($"------  Destruct:{GetType().Name}");
    }

//Now my code like this one
class ControlWithMapCreateAttach02 : ContentControl
{
private MapControl _cMapControl;

        protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
        {
            base.OnAttachedToVisualTree(e);
            Content = _cMapControl = new MapControl();
        }

        protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
        {
            base.OnDetachedFromVisualTree(e);
            var temp = _cMapControl;
            Content  = _cMapControl = null;
            temp?.Dispose();
        }

        ~ControlWithMapCreateAttach02() => Console.WriteLine($"------  Destruct:{GetType().Name}");
    }


    private Panel _cPanelRoot;
    public MainWindow()
    {
        InitializeComponent();
        Content = _cPanelRoot = new Panel();

        var timer = new DispatcherTimer(TimeSpan.FromSeconds(5), DispatcherPriority.Render, (sender, args) =>
        {
            var temp = _cPanelRoot.Children.OfType<IDisposable>().ToList();
            _cPanelRoot.Children.Clear();
            temp.ForEach(t => t.Dispose());

            _cPanelRoot.Children.AddRange(new Control[]
            {
                new MapControlEx(),
                new ControlEx(),
                new ControlOnDisposeWithMap00(),
                new ControlWithMapDisposeOnDetach01(),
                new ControlWithMapCreateAttach02(),
            });
            GC.Collect();
        });
    }
}

image

"ControlWithMapDisposeOnDetach01" —— It seems "MapControl" Dispose at first that can run a correct result.
"ControlWithMapDisposeOnDetach01" —— And It is reasonable to explain that there has been no memory leak。

"temp.ForEach(t => t.Dispose());" —— the logic is strange(In principle, i think all instances can execute destructors).

at last i changed the "temp.ForEach(t => t.Dispose());"
image
but result:
image

my logic is a bit confusing. Thank you for your enthusiastic help.

from mapsui.

xiaoliangde avatar xiaoliangde commented on July 4, 2024

If you can provide a minimal reproducible sample I will further investigate this.
“ControlOnDisposeWithMap00”——my earliest implementation method
“ControlWithMapCreateAttach02”——current implementation method
“ControlOnDisposeWithMap01”——The way I just had a sudden idea to add(surprisingly, the result was correct.And It is reasonable to explain that there has been no memory leak)

app.zip

from mapsui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.