Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Issue with OxyPlot in Xamarin.Forms

$
0
0

I am building a Xamarin.Forms app that needs to plot several graphs that update with new data every second. Eventually I will want the app to run on both Android and iOS but for now am working only with Android. I've found Oxyplot to be a good library to use, but I've encountered memory issues. To see if the problem was being caused by Oxyplot and not somewhere else in the code, I created a simple Xamarin.Forms app that has a single line series to which a new data point is added every second and ran it through the Profiler.

What I find is that the memory usage continues to slowly but steadily grow over time. According to the profiler, there are a number of System.Collections.Generic.Dictionary.Entry<System.IntPtr,System.Collections.Generic.List<System.WeakReference>>[] which continue to grow in size over time. With every call to OxyPlotModel.InvalidatePlot(true), the memory usage increases.

I am not sure where the problem lies, my Google searches have not turned up anything on point. But enough people use OxyPlot that if there was an underlying problem, then someone would have encountered it. Which leaves the problem lying with my code, I've included the code here, it's just about the most simple example of what I need the graph to do in my actual application.

namespace SimpleGraph
{
    public class MyPage : ContentPage
    {
        PlotModel myPlotModel = new PlotModel ();
        PlotView myPlotView = new PlotView(){
            Model = new PlotModel { Title = "Hello, Forms!" },
            VerticalOptions = LayoutOptions.Fill,
            HorizontalOptions = LayoutOptions.Fill,
        };
        public MyPage ()
        {
            int ctr = 3;


            myPlotView.Model = myPlotModel;
            Content = myPlotView;
            SetupGraphs ();
            Device.StartTimer (new TimeSpan (0, 0, 0, 1), () => {       

                ctr++;
                UpdateGraphs(ctr);

                myPlotView.Model.InvalidatePlot(true);

                return true;
            });
        }

        public void SetupGraphs()
        {
            Random r = new Random ();
            myPlotModel.Axes.Add (new LinearAxis { Position = AxisPosition.Bottom });
            myPlotModel.Axes.Add (new LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = 0 });

            LineSeries series1 = new LineSeries {
                MarkerType = MarkerType.Circle,
                MarkerSize = 4,
                MarkerStroke = OxyColors.White
            };
            series1.Points.Add (new DataPoint (0.0, r.Next(0, 10)));
            series1.Points.Add (new DataPoint (1.4, r.Next(0, 10)));
            series1.Points.Add (new DataPoint (2.0, r.Next(0, 10)));

            myPlotModel.Series.Add (series1); 
        }
        public void UpdateGraphs(int ctr)
        {
            Random r = new Random ();
            DataPoint dp = new DataPoint(ctr, r.Next (0, 10));

            var series1 = myPlotModel.Series [0] as LineSeries;
            series1.Points.Add (dp);

            if (series1.Points.Count > 30) {
                series1.Points.RemoveAt (0);
            }
        }
    }
}

I am new to mobile development, so I apologize if I've done something stupid, or am just using OxyPlot incorrectly.

Thanks for any assistance.


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>