My issue:
I'm processing streams of images in my app. Some of the images can be really big. So I need a way to tell whether I can process it at all with the remaining memory I have before I process an image. But how can I know how much remaining memory I can use?
My research results:
- I can know the total memory and user memory with this UIDevice extension.
- I can know the virtual, resident, wired, active, inactive, and free memory by this.
My logs:
2013-12-13 11:15:05.966 Total Memory: 505 MB User Memory: 434.3 MB
2013-12-13 11:15:05.967 Virtual: 348.7 MB Resident: 6.3 MB Free: 254.1 MB Inactive: 35 MB Active: 70.7 MB Wired: 70.6 MB
2013-12-13 11:15:57.742 Virtual: 530.2 MB Resident: 95.2 MB Free: 160.6 MB Inactive: 45 MB Active: 74.1 MB Wired: 72 MB
2013-12-13 11:16:41.320 Virtual: 569.2 MB Resident: 88 MB Free: 121 MB Inactive: 46.4 MB Active: 71.8 MB Wired: 76.3 MB
2013-12-13 11:16:46.254 Virtual: 612.9 MB Resident: 88.7 MB Free: 50.2 MB Inactive: 33 MB Active: 64.2 MB Wired: 117.7 MB
2013-12-13 11:16:49.536 Virtual: 525.6 MB Resident: 89.9 MB Free: 3.6 MB Inactive: 33.8 MB Active: 154.3 MB Wired: 71.7 MB
2013-12-13 11:16:50.854 Virtual: 568.9 MB Resident: 90.1 MB Free: 139.5 MB Inactive: 35 MB Active: 64.1 MB Wired: 71.2 MB
2013-12-13 11:16:56.358 Virtual: 613.8 MB Resident: 92.6 MB Free: 51.3 MB Inactive: 35.1 MB Active: 107.1 MB Wired: 71.5 MB
2013-12-13 11:17:05.034 Virtual: 658.4 MB Resident: 83.9 MB Free: 48 MB Inactive: 30.5 MB Active: 62.4 MB Wired: 70.1 MB
2013-12-13 11:17:15.196 Virtual: 587.4 MB Resident: 143.1 MB Free: 194 MB Inactive: 6.2 MB Active: 15.1 MB Wired: 69.5 MB
2013-12-13 11:17:18.483 Virtual: 629.3 MB Resident: 145.2 MB Free: 97.2 MB Inactive: 6.3 MB Active: 47.3 MB Wired: 92.3 MB
2013-12-13 11:17:21.098 Virtual: 675.5 MB Resident: 145.2 MB Free: 52.7 MB Inactive: 24.2 MB Active: 51.6 MB Wired: 69.3 MB
2013-12-13 11:17:22.133 Received memory warning.
2013-12-13 11:17:22.187 Virtual: 711.3 MB Resident: 172.1 MB Free: 36.1 MB Inactive: 20.4 MB Active: 1.8 MB Wired: 114.3 MB
2013-12-13 11:17:22.477 Received memory warning.
2013-12-13 11:17:22.480 Virtual: 568.1 MB Resident: 124.7 MB Free: 194.6 MB Inactive: 20.9 MB Active: 3.3 MB Wired: 112.4 MB
2013-12-13 11:17:22.571 Virtual: 522.6 MB Resident: 36.2 MB Free: 282.4 MB Inactive: 20.9 MB Active: 3.9 MB Wired: 66.8 MB
My questions:
- What does the amount of virtual memory mean on iOS? As I know, iOS does not have swap so there is not hard disk involved. But I found it CAN and most of the time DOES exceed user memory and even total memory.
- What's the relationship between resident memory and {wired, active, inactive} memories? I know the relationships among the latter group.
- Why is the sum of {wired, active, inactive, free} memory inconsistent? What's the relations between this sum and total memory or user memory or virtual memory?
- How to calculate the amount of remaining memory that I can use in my app?
For the last question above, I tried using only free memory. I'm sure it is not right since most of the time free memory is very little and what I can use in fact can easily exceed that without any issues. I also tried (free memory + inactive memory). Still seems not quite right since I can use more than that without issues in my experiments. One difficulty involved here is that when my app is active and needs more memory the system can kill other inactive apps to spare more memory to my app. I need to take this later-spared amount into account when calculating the remaining memory that I can use.