I'm developing an iOS app for drawing on a simple canvas. The app manages one canvas per time (layers are managed differently). I didn't use 3rd party libraries and all components are Apple friendly (canvas is PencilKit for example) while the UI is all SwiftUI. The app never crashed but while testers are playing on it, the dirty memory grows and the app is noticeably slower.
The Instruments record shows a grew in correct memory when the app starts, but the memory used never decreases, even when the action using memory is finished. This causes a constant increase in dirty memory.
I'm working on simulator and testers on real devices like iPad 12.9 4th generation.
I heavily work with Dispatchers, UIImages and CIImages.
Attached part of the Instruments VM Tracker of dirty memory.
VM Tracker
VIRTUAL RESIDENT DIRTY SWAPPED VOLATILE NONVOL EMPTY REGION
REGION TYPE SIZE SIZE SIZE SIZE SIZE SIZE SIZE COUNT (non-coalesced)
=========== ======= ======== ===== ======= ======== ====== ===== =======
Activity Tracing 256K 40K 40K 0K 0K 40K 0K 1
CG raster data 46.3M 46.2M 46.2M 140K 0K 0K 0K 21
CoreAnimation 93.5M 92.5M 92.5M 1036K 0K 108K 0K 37
CoreData 8K 8K 8K 0K 0K 0K 0K 1
CoreData Object IDs 4100K 4K 4K 0K 0K 0K 0K 2
CoreImage 114.6M 114.6M 114.6M 0K 0K 0K 0K 8
CoreUI image data 588K 424K 424K 164K 0K 0K 0K 3
Foundation 1040K 1028K 1028K 0K 0K 0K 0K 4
IOSurface 28.1M 23.7M 23.7M 0K 0K 23.7M 0K 10
Image IO 9952K 5984K 5984K 3968K 0K 0K 0K 4
Kernel Alloc Once 8K 8K 8K 0K 0K 0K 0K 1
MALLOC guard page 32K 0K 0K 0K 0K 0K 0K 8
MALLOC metadata 220K 160K 160K 20K 0K 0K 0K 11
MALLOC_LARGE 7756K 5520K 5520K 2116K 0K 0K 0K 25 see MALLOC ZONE table below
MALLOC_LARGE (empty) 8464K 7676K 7676K 520K 0K 0K 0K 13 see MALLOC ZONE table below
MALLOC_LARGE_REUSABLE 30.3M 27.2M 11.7M 0K 0K 0K 0K 10 see MALLOC ZONE table below
MALLOC_NANO 128.0M 5756K 5756K 1268K 0K 0K 0K 1 see MALLOC ZONE table below
MALLOC_NANO (empty) 384.0M 0K 0K 0K 0K 0K 0K 1 see MALLOC ZONE table below
MALLOC_SMALL 112.0M 6728K 6728K 388K 0K 0K 0K 14 see MALLOC ZONE table below
MALLOC_TINY 24.0M 2772K 2772K 8K 0K 0K 0K 24 see MALLOC ZONE table below
Performance tool data 448K 432K 432K 16K 0K 0K 0K 8 not counted in TOTAL below
SQLite page cache 512K 508K 508K 4K 508K 0K 0K 8
STACK GUARD 56.0M 0K 0K 0K 0K 0K 0K 7
Stack 11.0M 400K 364K 44K 0K 0K 0K 7
Stack (reserved) 520K 0K 0K 0K 0K 0K 0K 1 reserved VM address space (unallocated)
Stack Guard 4K 0K 0K 0K 0K 0K 0K 1
VM_ALLOCATE 17.8M 1360K 1360K 148K 0K 0K 0K 44
VM_ALLOCATE (reserved) 4K 0K 0K 0K 0K 0K 0K 1 reserved VM address space (unallocated)
__DATA 16.9M 9751K 3025K 664K 0K 0K 0K 397
__DATA_CONST 33.9M 29.8M 772K 742K 0K 0K 0K 391
__DATA_DIRTY 20K 19K 19K 0K 0K 0K 0K 6
__FONT_DATA 4K 0K 0K 0K 0K 0K 0K 1
__LINKEDIT 402.5M 176.6M 0K 0K 0K 0K 0K 15
__OBJC_RO 54.0M 30.0M 0K 0K 0K 0K 0K 1
__OBJC_RW 2592K 1610K 18K 0K 0K 0K 0K 1
__TEXT 392.7M 102.7M 28K 0K 0K 0K 0K 407
__UNICODE 588K 336K 0K 0K 0K 0K 0K 1
mapped file 268.3M 32.2M 24K 0K 0K 0K 0K 221
shared memory 233.1M 1532K 1532K 231.6M 0K 4K 0K 23
unused but dirty shlib __DATA 340K 115K 115K 226K 0K 0K 0K 279
=========== ======= ======== ===== ======= ======== ====== ===== =======
TOTAL 2.4G 726.0M 331.5M 242.7M 508K 23.8M 0K 2011
TOTAL, minus reserved VM space 2.4G 726.0M 331.5M 242.7M 508K 23.8M 0K 2011
I need to understand why memory is not deallocated when a SwiftUI's view disappears.
This is the simple flow:
start the app 20MB memory
open canvas 200MB memory
close canvas (should be 20MB again) 200 MB
So: is there a way in Instruments to find exactly which Object retains dirty memory?
Thanks