How do I identify memory leaks?

Guys,

As the subject says, how do i identify memory leaks?

My logger on a Panda runs out of memory after 15 minutes, but i have no clue where that is happening.
I see in the GC messages that BINARY_BLOB_HEAD is always increasing.

Anyone can shed some light on this?

I bet you it is data left in serial receive buffer :slight_smile: This is the most common one for BINARY_BLOB

I disabled the Serial code part, but BINARY_BLOB is still increasing, though slower.

Ok, I disabled practically everything in my app except the call to update the 3.6" screen. The BINARY_BLOB keeps increasing. When I comment out the screen updates, the memory leak disappears.

Could it be there’s an memory leak with the 3.6" display driver? Any recommendations on how to update this screen every second?

very strange. Can we get a small code example to take a look. Perhaps start a new project that only has the driver

i will get you a code sample in the morning (my morning), it’s midnight here…need to grab some sleep

ok, it’s not the display driver after all.

The problem lies in the following piece of code.



    /// <summary>
    /// Return Instant Gas consumption in m3/h
    /// </summary>
    static private long gacons;
    static public double GACons
    {
      get
      {
        if (gacons != 0 & gacons < 15000)
        { return (.01 / ((double)gacons / 1000) * 3600); }
        else
        { return 0; }
      }
      set { gacons = (long)value; }
    }

    /// <summary>
    /// Return Water Consumption in L/Min
    /// </summary>
    static private long wacons;
    static public double WACons
    {
      get
      {
        if (wacons != 0)
        {
          return (60D * .5D) / ((double)wacons / 1000D);
        }
        else
        { return 0; }
      }
      set { wacons = (long)value; }
    }

Every time I update my display (and getting WACons or GACons), the BINARY_BLOB size increases and never goes down again, which results in an out of memory after a while.

Any idea?

It can be hard to find. I don’t think it is related to these readings directly but related to LCD updating. what is happening there?

Keep narrowing down your application until you find out what is happening.
Make sure no arrays or objects or containers (like array) list keep growing forever.
Also, add Debug.GC(true); somewhere in your application loop to claim unused memory.

Can you make a complete example with the code you posted to demonstrate this?

main()
{
 /// do somethign

  while(true)
  {
    // run suspected code
    Debug.GC(true);
    Thread.Sleep(1);
  }
}

Mike,

I have a DS1307 triggering an interrupt at 1hz. When the trigger fires I update the screen as follows:


    /// <summary>
    /// Layout of screen 2
    /// </summary>
    static void screen2()
    {
      //Title
      paint.Print(2, 0, "e-Home Data Logger");

      //Table
      paint.RectSE(40, 18, 127, 27); //top rectangle
      paint.RectSE(0, 27, 127, 63); //big rectangle
      paint.Line(0, 36, 127, 36); //horizontal line 1
      paint.Line(0, 45, 127, 45); //horizontal line 2
      paint.Line(0, 54, 127, 54); //horizontal line 3
      paint.Line(40, 18, 40, 63); //vertical line 1
      paint.Line(80, 18, 80, 63); //vertical line 2

      //Header
      paint.PrintSmall(42, 19, "Counter");
      paint.PrintSmall(82, 19, "Consumption");

      //Labels
      paint.PrintSmall(2, 28, "E-Day");
      paint.PrintSmall(2, 37, "E-Night");
      paint.PrintSmall(2, 46, "Gas");
      paint.PrintSmall(2, 55, "Water");
      DisplayFlush();
    }

    /// <summary>
    /// Changing values of screen 2
    /// This is called every second
    /// </summary>
    public static void updatescreen2()
    {
      if (timerenabled)
      {
        // Time
        DateTime t = DateTime.Now;
        paint.PrintSmall(1, 19,  t.TimeOfDay.ToString().Substring(0,8));
        paint.PrintSmall(1, 10, FEZ_Panda_LCD_128x64.Program.uptime.ToString());

        //Counter Data
        paint.PrintSmall(42, 28, "      ");
        paint.PrintSmall(42, 28, CounterCollection.EDCounter.ToString("F3"));
        paint.PrintSmall(42, 37, "      ");
        paint.PrintSmall(42, 37, CounterCollection.ENCounter.ToString("F3"));
        paint.PrintSmall(42, 46, "       ");
        paint.PrintSmall(42, 46, CounterCollection.GACounter.ToString("F3"));
        paint.PrintSmall(42, 55, "        ");
        paint.PrintSmall(42, 55, CounterCollection.WACounter.ToString("F3"));

        //Consumption Data
        if (CounterCollection.HighRate)
        {
          paint.PrintSmall(82, 28, "           ");
          paint.PrintSmall(82, 37, "           ");
          paint.PrintSmall(82, 28, CounterCollection.EDCons.ToString("F0") + " Watt");
        }
        else
        {
          paint.PrintSmall(82, 28, "           ");
          paint.PrintSmall(82, 37, "           ");
          paint.PrintSmall(82, 37, CounterCollection.EDCons.ToString("F0") + " Watt");
        }

        //shit happens here. If these 4 lines are commented out, it keeps running nice.
        paint.PrintSmall(82, 46, "      ");
        paint.PrintSmall(82, 46, CounterCollection.GACons.ToString("F1") + " m3/h");
        paint.PrintSmall(82, 55, "        ");
        paint.PrintSmall(82, 55, CounterCollection.WACons.ToString("F1") + " L/m");

        if (CounterCollection.HighRateTest)
        {
          paint.ClearCirlcle(35, 41, 2);
          paint.Cirlcle(35, 32, 2);
        }
        else
        {
          paint.ClearCirlcle(35, 32, 2);
          paint.Cirlcle(35, 41, 2);
        }
        DisplayFlush();
      }
    }

    static void DisplayFlush()
    {
      FEZ_Extensions.Large128x64Display.Flush(paint.vram);
    }


Maybe I’m pushing too much data to the sceen?

PS: The Debug.GC(true) didn’t help.

Like requested, please provide a simple demo demonstrating the issue … a demo that is 10 lines of code and not related to a display or any hardware for that matter.

Gus, I tried but i can’t replicate the behavior I’m seeing.
I’ll keep on digging though. This is one of the last outstanding issues I’m facing before i can put this in production.