Excessive dirty memory - ios

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

Related

filter the packets Wireshark with size

I am trying to solve the below question:
filter the UDP packets having a size equal to 242 bytes.
I looked to this answer udp.length==209 set a filter of packet length in wireshark, but instead of getting packets with length 209 bytes I get packets with length 243 bytes.
screenshot. can anyone explain?
Your image shows a packet like
Frame 243 bytes
'-> Ethernet
'-> IPv4
'-> UDP
'-> Dropbox LAN Sync
Ethernet will be 14 bytes with 6 per src/dst MAC address and 2 bytes for Ethertype.
The IPv4 header will be a minimum of 20 bytes, but could be more with options. It just so happens to be 20 here.
Eth 14 bytes + IP 20 bytes = 34 bytes
243 bytes - 34 bytes of Eth/IP = 209 bytes of UDP data

Memory Leak inspection with Windbg - Increase in heap size doesn't reflect in Heap detail

I'm trying to detect a memory leak location for a certain process via Windbg, and have come across a strange problem.
Using windbg, I've created 2 memory dump snapshots - one before and one after the leak, that showed an increase of around 20MBs (detected via Performance Monitor - private bytes). it shows that there is indeed a similar size difference in one of the heaps before and after the leak (Used with the command !heap -s):
Before:
Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast
(k) (k) (k) (k) length blocks cont. heap
-----------------------------------------------------------------------------
03940000 08000002 48740 35312 48740 4372 553 9 5 2d LFH
External fragmentation 12 % (553 free blocks)
03fb0000 08001002 7216 3596 7216 1286 75 4 8 0 LFH
External fragmentation 35 % (75 free blocks)
05850000 08001002 60 16 60 5 2 1 0 0
...
After:
Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast
(k) (k) (k) (k) length blocks cont. heap
-----------------------------------------------------------------------------
03940000 08000002 64928 55120 64928 6232 1051 26 5 51 LFH
External fragmentation 11 % (1051 free blocks)
03fb0000 08001002 7216 3596 7216 1236 73 4 8 0 LFH
External fragmentation 34 % (73 free blocks)
05850000 08001002 60 16 60 5 2 1 0 0
...
See the first Heap (03940000) - there is a difference in committed KBs of around 55120 - 35312 = 19808 KB = 20.2 MB.
However, when I inspected that heap with (!heap -stat -h 03940000), it displays the following for both dump files:
size #blocks total ( %) (percent of total busy bytes)
3b32 1 - 3b32 (30.94)
1d34 1 - 1d34 (15.27)
880 1 - 880 (4.44)
558 1 - 558 (2.79)
220 1 - 220 (1.11)
200 2 - 400 (2.09)
158 1 - 158 (0.70)
140 2 - 280 (1.31)
...(rest of the lines show no difference)
size #blocks total ( %) (percent of total busy bytes)
3b32 1 - 3b32 (30.95)
1d34 1 - 1d34 (15.27)
880 1 - 880 (4.44)
558 1 - 558 (2.79)
220 1 - 220 (1.11)
200 2 - 400 (2.09)
158 1 - 158 (0.70)
140 2 - 280 (1.31)
...(rest of the lines show no difference)
As you can see, there is hardly a difference between the two, despite the abovementioned 20MB size difference.
Is there an explanation for that?
Note: I have also inspected the Unmanaged memory using UMDH - there wasn't a noticeable size difference there.

Xcode crashes when I try to create a outlet from a UIWebview to Header file

I am currently using Xcode 5 on my Macbook Pro which is running OSX Yosemite. I am trying to create an outlet from a UIWebview to a header file. When I let go of my mouse to create the connection, Xcode freezes and crashes. This is repeatedly happening no matter what I have done. I have tried restarting Xcode and my Macbook. Does anyone else have this problem or know how to fix it? Thanks!
I have added the logs from the crash(I couldn't include the backtrace because it exceeded the maximum characters in the body)
Process: Xcode [1631]
Path: /Applications/Xcode.app/Contents/MacOS/Xcode
Identifier: com.apple.dt.Xcode
Version: 5.1.1 (5085)
Build Info: IDEFrameworks-5085000000000000~10
App Item ID: 497799835
App External ID: 520942841
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: Xcode [1631]
User ID: 502
Date/Time: 2014-06-26 21:50:34.616 -0400
OS Version: Mac OS X 10.10 (14A238x)
Report Version: 11
Anonymous UUID: 6D3E70F1-91EB-79A5-FAB6-2929D7E8A66F
Sleep/Wake UUID: C86C5452-A655-43AA-9D11-92BE0F080F3E
Time Awake Since Boot: 11000 seconds
Time Since Wake: 510 seconds
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Application Specific Information:
ProductBuildVersion: 5B1008
ASSERTION FAILURE in /SourceCache/DVTFrameworks/DVTFrameworks-5074/DVTKit/ViewControllers/DVTControllerContentView.m:280
Details: <DVTControllerContentView: 0x7fe687d170a0> can only have one subview, its contentView
Object: <DVTControllerContentView: 0x7fe687d170a0>
Method: -addSubview:
Thread: <NSThread: 0x7fe682d086a0>{number = 1, name = main}
Hints: None
External Modification Summary:
Calls made by other processes targeting this process:
task_for_pid: 1
thread_create: 0
thread_set_state: 0
Calls made by this process:
task_for_pid: 0
thread_create: 0
thread_set_state: 0
Calls made by all processes on this machine:
task_for_pid: 4286
thread_create: 0
thread_set_state: 0
VM Region Summary:
ReadOnly portion of Libraries: Total=404.2M resident=289.4M(72%) swapped_out_or_unallocated=114.9M(28%)
Writable regions: Total=1.2G written=71.9M(6%) resident=141.4M(11%) swapped_out=0K(0%) unallocated=1.1G(89%)
REGION TYPE VIRTUAL
=========== =======
Activity Tracing 2048K
CG backing stores 4084K
CG image 19.3M
CG raster data 92K
CG shared images 1324K
CoreAnimation 880K
CoreAnimation (reserved) 12K reserved VM address space (unallocated)
CoreData Object IDs 4100K
CoreImage 264K
CoreUI image data 180K
Dispatch continuations 4096K
Foundation 16K
IOKit 13.8M
IOKit (reserved) 4K reserved VM address space (unallocated)
Image IO 700K
JS JIT generated code 8K
JS JIT generated code (reserved) 1.0G reserved VM address space (unallocated)
Kernel Alloc Once 8K
MALLOC 114.7M
MALLOC (admin) 32K
Memory Tag 242 12K
Memory Tag 249 156K
Memory Tag 251 80K
OpenCL 16K
OpenGL GLSL 256K
SQLite page cache 4352K
STACK GUARD 56.1M
Stack 15.7M
VM_ALLOCATE 16.9M
WebKit Malloc 464K
__DATA 60.1M
__GLSLBUILTINS 2588K
__IMAGE 528K
__LINKEDIT 100.5M
__TEXT 304.0M
__UNICODE 544K
mapped file 152.6M
shared memory 68K
=========== =======
TOTAL 1.9G
TOTAL, minus reserved VM space 879.9M
Model: MacBookPro7,1, BootROM MBP71.0039.B0E, 2 processors, Intel Core 2 Duo, 2.4 GHz, 8 GB, SMC 1.62f7
Graphics: NVIDIA GeForce 320M, NVIDIA GeForce 320M, PCI, 256 MB
Memory Module: BANK 0/DIMM0, 4 GB, DDR3, 1067 MHz, 0x859B, 0x435435313236344243313036372E4D313646
Memory Module: BANK 1/DIMM0, 4 GB, DDR3, 1067 MHz, 0x859B, 0x435435313236344243313036372E4D313646
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0x8D), Broadcom BCM43xx 1.0 (5.106.98.100.24)
Bluetooth: Version 4.3.0d49 14130, 3 services, 16 devices, 1 incoming serial ports
Network Service: Wi-Fi, AirPort, en1
Serial ATA Device: Hitachi HTS545032B9SA02, 320.07 GB
Serial ATA Device: HL-DT-ST DVDRW GS23N
USB Device: Built-in iSight
USB Device: Internal Memory Card Reader
USB Device: BRCM2046 Hub
USB Device: Bluetooth USB Host Controller
USB Device: IR Receiver
USB Device: Apple Internal Keyboard / Trackpad
Thunderbolt Bus:
I think you should declare a IBOutlet in your header file. And then in Storyboard you try to connect the header file.
Have you ever tried to clean your product(Product->Clean)?
Then, open the xib file as source code and delete your IBOutlet info. Maybe XCode get confused about your IBOutlet.

Access permissions of memory segment

As we all know, memory layout has four segments as follows
Heap
Data Segment Read/Write permission
code segment Read/Execute permission
Stack
I would like to know the access permission for heap and stack segments.
I have written simple x86 OS to load elf image.
Which the roughly layout of the four segment is as:
Code segment
Data segment
Heap
Stack
You can get the layout in linux by:
sudo pmap <pid>
0000000000400000 384K r-x-- /usr/local/bin/tmux
0000000000660000 8K rw--- /usr/local/bin/tmux
0000000000662000 108K rw--- [ anon ]
0000000001242000 132K rw--- [ anon ]
0000000001263000 2868K rw--- [ anon ]
00000037ba400000 128K r-x-- /lib64/ld-2.12.so
...
00007fff4f411000 84K rw--- [ stack ]
00007fff4f55c000 4K r-x-- [ anon ]
ffffffffff600000 4K r-x-- [ anon ]
The first segment is for executable image. Second is for the read only data. Then goes the heap. The heap in linux which allocated by malloc was called Anonymous segment. In the address near 00008000 00000000 is the stack addresses.
Which stack grow downside, and heap grow upside from the address after the data segment of program.
Then you could found the permission of the heap and stack are all read/write permission.
Ps. I'm not quite sure why there are so many Anonymous segment as I haven't go to so detail of linux for a long time. But in my x86 simple OS, heap start after data segment with alignment and padding.

Security mp_init and init memory leaks

I build an iPad application and profile it with XCode, I got memory leaks in Security framework, following is some of them from Instruments:
Leaked Object # Address Size Responsible Library Responsible Frame
Malloc 128 Bytes,4 < multiple > 512 Bytes Security mp_init
Malloc 128 Bytes, 0x824aa70 128 Bytes Security mp_init
Malloc 128 Bytes, 0x824a9f0 128 Bytes Security mp_init
Malloc 128 Bytes, 0x824a970 128 Bytes Security mp_init
Malloc 128 Bytes, 0x824a8f0 128 Bytes Security mp_init
Leaked Object # Address Size Responsible Library Responsible Frame
Malloc 16 Bytes,4 < multiple > 64 Bytes Security init
Malloc 16 Bytes, 0x824a550 16 Bytes Security init
Malloc 16 Bytes, 0x824a540 16 Bytes Security init
Malloc 16 Bytes, 0x82493d0 16 Bytes Security init
Malloc 16 Bytes, 0x8237ca0 16 Bytes Security init
and some of the details:
# Category Event Type Timestamp RefCt Address Size Responsible Library Responsible Caller
0 Malloc 128 Bytes Malloc 00:02.240.888 1 0x824aa70 128 Security mp_init
# Category Event Type Timestamp RefCt Address Size Responsible Library Responsible Caller
0 Malloc 16 Bytes Malloc 00:02.240.886 1 0x824a550 16 Security init
I use ASIHTTP, FMDatabase and some other frameworks(I do not think they will cause the problem) in my application, and no Security framework in "Link Binary With Libraries", so I have no idea what's going wrong.
Can anyone figure out what may cause the leaks?

Resources