; Compare a menu baseline with retained level data and reusable Type pools. ; Run with the debugger too: pause at any stage and use its Memory tab. Type Pickup Field value End Type Global baseline_pickups:Long Global baseline_lists:Long Global baseline_banks:Long Function ReadCount:Long(row,key$) Local value=JsonObjectGet(row,key) Local result:Long=Long(JsonText(value)) FreeJson value Return result End Function Function ReadName$(row) Local value=JsonObjectGet(row,"id") Local result$=JsonText(value) FreeJson value Return result End Function Function ShowMemory(label$,set_baseline=False) ; Scalars are captured before result strings and JSON parsing allocate. Local report$=MemorySnapshot$() Local root=ParseJson(report) Local categories=JsonObjectGet(root,"categories") Print "" Print label For index=0 To JsonCount(categories)-1 Local row=JsonArrayGet(categories,index) Local name$=ReadName(row) Local active:Long=ReadCount(row,"activeCount") Local change:Long=0 If name="types" If set_baseline Then baseline_pickups=active change=active-baseline_pickups Else If name="lists" If set_baseline Then baseline_lists=active change=active-baseline_lists Else If name="banks" If set_baseline Then baseline_banks=active change=active-baseline_banks EndIf If name="types" Or name="lists" Or name="banks" Print name+": active="+active+", change="+change Print " Accounted bytes="+ReadCount(row,"accountedBytes") Print " Deleted retained="+ReadCount(row,"deletedRetainedCount") EndIf FreeJson row Next FreeJson categories FreeJson root ; report and parsing temporaries are released on return. End Function Function FillLevel() For index=1 To 20 Local item.Pickup=New Pickup item\value=10 Next ; Pickups remain in their implicit Type list after this function returns. End Function Print "Memory reporting: baseline, retention, cleanup, and reuse" Print "Every change below is relative to the menu baseline." ; Warm diagnostic parsing before setting the measured baseline. ShowMemory "Warm-up" ShowMemory "Menu baseline",True FillLevel Local inventory:Int List=New List inventory.Add(10) inventory.Add(20) Local level_bank=CreateBank(4096) ShowMemory "Level retained: 20 pickups, one List and one Bank" ; Keep one alias alive across logical deletion. Local alias.Pickup=First Pickup Delete Each Pickup ShowMemory "Pickups deleted: one alias retains its storage" alias=Null Delete inventory inventory=Null FreeBank level_bank level_bank=0 ShowMemory "Cleanup: active counts return to baseline" For cycle=1 To 3 FillLevel Delete Each Pickup ShowMemory "Reuse cycle "+cycle Next Print "" Print "Type pool bytes remain reserved and are reused by later cycles." Print "Temporary report Strings can raise the lifetime peak." Print "Growth is a measurement, not proof of a leak." Print "" Print "Press any key to close the example" WaitKey End