Checking if I have a memory leak

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

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.

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

@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.

[quote=475525:@Beatrix Willius]@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.[/quote]

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

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.

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

an instruments tutorial would be a very good idea!

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

it can detect memory leaks.