Code-Artist/AGauge

Reduce Memory Usage/Leak

soul4soul opened this issue · 2 comments

I have two of these gauges on a form updating every 100ms with new values. Every time the gauge value is updated the memory usage is increasing. I noticed the memory will reach about 210MB before a garbage collection occurs.

In comparison I replaced the AGauage with a DevExpress gauge and did not experience the issue. Memory usage stayed steady at 20MB with no garbage collections occurring.

Below is a copy of the code on the main (and only form) in the program. (NOTE: This is currently the code for the devexpress gauges but it was very identical for the Aguage. I used gauge labels to display the degree and velocity.)

namespace TSDemoGUI
{
    public partial class Form1 : Form
    {
        private int selectedDevice;
        private double angle;
        private double ChannelOneVelocity;
        private double ChannelOneVelocityRPM;
        private double ChannelOneVelocityRPS;
        private Random value;

        public Form1()
        {
            InitializeComponent();
            // Set Up Synchro Device
            selectedDevice = 0;
            //SRTestSystemDevice.InitDevice(selectedDevice);
            //SRTestSystemDevice.SetUpDevice(selectedDevice);
            value = new Random();
            timerUpdateAngles.Enabled = true;
        }

        private void timerUpdateAngles_Tick(object sender, EventArgs e)
        {
            // Angle
            angle = value.Next(0, 359);
            arcScaleComponent2.Value = Convert.ToUInt32(angle);
            labelComponent3.Text = angle.ToString("000.000");

            // Velocity
            ChannelOneVelocityRPS = value.NextDouble() * 15;
            arcScaleComponent3.Value = Convert.ToSingle(ChannelOneVelocityRPS);
            labelComponent6.Text = ChannelOneVelocityRPS.ToString("+00.00;-00.00; 00.00");
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            timerUpdateAngles.Enabled = false;
        }
    }
}

Image of Form
capture

This happens because the graphics objects used to draw the gauge aren't disposed after they are used. Eventually the garbage collector frees them all. I fixed this in a fork: https://github.com/PaulMartinsen/AGauge

Thank you, great job.