You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Main/Memory Leak Testing
Class App Inherits WebApplication
EventHandler Function HandleURL(Request As WebRequest, Response As WebResponse) As Boolean
If Request.Path = "test" Then
Response.Write "Hello World"
Response.Status = 200
Return True
End If
End EventHandler
EventHandler Sub Opening(args() as String)
StartMemoryStatisticsTimer
End EventHandler
Protected Sub StartMemoryStatisticsTimer()
// start timer to print memory stats
Static t As MemoryStatisticsTimer
t = New MemoryStatisticsTimer
t.Period = 2000
t.Mode = timer.ModeMultiple
// query something, so it initializes
Dim RuntimeObjectIterator As Runtime.ObjectIterator = Runtime.IterateObjects
End Sub
End Class
Class MemoryStatisticsTimer Inherits Timer
EventHandler Sub Action()
// this stats show in Terminal when app runs as Standalone or you may see in debugger in messages
System.DebugLog Str(Runtime.ObjectCount)+" objects using "+Str(runtime.MemoryUsed / 1024, "0.0")+" KB"
counter = counter + 1
If counter Mod 10 = 0 Then
// show stats every 10 seconds
DumpObjects
End If
End EventHandler
Private Sub DumpObjects()
// find all objects
Dim objects() As Variant
Dim RuntimeObjectIterator As Runtime.ObjectIterator = Runtime.IterateObjects
While RuntimeObjectIterator.MoveNext
objects.append RuntimeObjectIterator.Current
Wend
// for macOS, iOS and Linux compact memory and print stats
#If Not TargetWindows
Dim m As New MemoryStatisticsMBS
m.Compact
System.DebugLog Str(m.bytesFree)+" free, "+Str(m.bytesTotal)+" total"
// free bytes shows how much space is available to be reassigned for new objects without requesting new memory pages from OS
#EndIf
// make a table with statistics
Dim counts As New Dictionary
Dim tab As String = Chr(9)
For Each o As Variant In objects
Dim t As Introspection.TypeInfo = Introspection.GetType(o)
Dim s As String
If t = Nil Then
// internal objects
s = "internal"
Elseif o.IsArray Then
s = "array"
Else
s = t.name
End If
Dim key As String = Str(VarType(o))+tab+s
counts.Value(key) = counts.Lookup(key, 0).IntegerValue + 1
Next
// make lines and sort, then print
Dim lines() As String
For Each key As Variant In counts.keys
lines.Append key.StringValue + tab + counts.value(key)
Next
lines.Sort
For Each line As String In lines
Print line
Next
End Sub
Property Private counter As Integer
End Class