Checking if I have a memory leak

  1. 2 weeks ago

    Jim S

    Feb 14 Central Florida

    I have a program that I run in the background that freezes after running for a day or two.
    I have not been able to track down the cause of the freeze and am wondering if there is a
    memory leak. My knowledge of 'Instruments' is less than rudimentary but I believe both it
    and Activity Monitor show increasing memory use.

    Here is a simple example.

    Set up a window with a Listbox and Timer and set the Timer to Run Mode - Multiple every 2 seconds.

    Place the following code in the Timer Run event:

    Listbox1.RemoveAllRows
    
    For i As Integer = 0 To 10
      Listbox1.AddRow Str(i)
    Next

    Run the program. Memory use seems to continually increase but I am not sure I'm on the right track.

    Can anyone confirm whether this is normal or shows a leak before I submit a feedback report?

    Thanks for any help.

    Jim

    MacOS 10.15.3 Xojo 2019 r3.1

  2. Beatrix W

    Feb 14 Pre-Release Testers, Third Party Store Europe (Germany)

    I don't see a memory leak with the code you posted.

    What exactly do you see in Instruments? It should tell you something about leaked objects and then you need to dig into those objects.

    Activity Viewer shows the total memory used including memory fragmentation. That's different from what Instruments shows. Instruments is your best bet to find out what is causing the memory leak.

  3. Markus R

    Feb 14 Pre-Release Testers, Xojo Pro Europe / Germany / Lower Saxon...
    Edited 2 weeks ago

    oh, its build in Runtime.ObjectCount check this first.

    example to track object count with a shared property.

    example

    Sub Open() Handles Open
      Var a As New Class1
      Var b As New Class1
      
      'should be 2
      System.DebugLog(Class1.Count.ToString)
      
      a = Nil
      b = Nil
      
      'should be 0
      System.DebugLog(Class1.Count.ToString)
    End Sub

    Class1

    Public Shared Property Count as Integer = 0
    Public Sub Constructor()
      Class1.Count = Class1.Count + 1 
    End Sub
    Public Sub Destructor()
      Class1.Count = Class1.Count - 1 
    End Sub
  4. Beatrix W

    Feb 14 Pre-Release Testers, Third Party Store Europe (Germany)

    @Markus Rauch: I don't know about your code but mine is a bit more complex.

    You can iterate over Runtime.ObjectCount to get the stuff that is currently in use. But that doesn't tell you if anything IN Xojo leaks.

    Only with Instruments you see the entrails of what your app is doing.

  5. Markus R

    Feb 14 Pre-Release Testers, Xojo Pro Europe / Germany / Lower Saxon...

    @Beatrix W @Markus Rauch: I don't know about your code but mine is a bit more complex.

    You can iterate over Runtime.ObjectCount to get the stuff that is currently in use. But that doesn't tell you if anything IN Xojo leaks.

    Only with Instruments you see the entrails of what your app is doing.

    ahh that is nice. agree , if that looks good then the reason is somewhere else.

  6. Beatrix W

    Feb 15 Pre-Release Testers, Third Party Store Europe (Germany)

    Would a tutorial for Instruments be helpful? I'm not an expert but I get by. This week I had some fun with a bug where my app got slower and slower after a while. With Instruments I was able to see that the MBS SQLite plugin made the problem.

  7. Jim S

    Feb 15 Central Florida

    Thanks for the replies.

    What I noticed when I tried to use Instruments/Leaks was the All Heap & Synonymous VM and All Heap Allocations & Transient continue to increase. I don't know if that's indicative of a problem or memory leak but since I'm at the limit of my Instruments knowledge I thought I'd see if anyone else considered this normal.

    I'll take a look at Runtime.ObjectCount.

    Jim

  8. last week

    Mark C

    Feb 15 Pre-Release Testers, Xojo Pro Spain 03170

    an instruments tutorial would be a very good idea!

  9. Derk J

    Feb 15 Pre-Release Testers, Xojo Pro
    Edited last week

    You can try XojoInstruments
    https://github.com/kmaehashi/XojoInstruments

    it can detect memory leaks.

or Sign Up to reply!