Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much remaining memory can I use?

Tags:

memory

ios

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:

  1. I can know the total memory and user memory with this UIDevice extension.
  2. 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:

  1. 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.
  2. What's the relationship between resident memory and {wired, active, inactive} memories? I know the relationships among the latter group.
  3. 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?
  4. 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.

like image 520
an0 Avatar asked Dec 13 '13 17:12

an0


People also ask

How much of memory should be used up?

If your RAM usage is higher than 30% at an idle state, you might be facing issues like lagging, random freezing, overheating, or programs/apps not responding. Many users reported 80-90% memory usage when no programs were opened, which is unacceptable.

How much of my physical memory should be used?

Using 30 - 38% of your RAM is normal. On many computers that is about average.

How do I know how much RAM I can hold?

Here's how: Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab to see current RAM usage displayed in the Memory box, and total RAM capacity listed under Physical Memory.

How much memory usage percentage is normal?

Is that normal? Yup. Windows puts stuff in ram before you actually need it to speed up your PC(stuff like the mail app, frequently used programs, etc).


1 Answers

Question 1:

You are confusing virtual memory with the concept of swapping. It's a common mistake.

Virtual memory is the total amount of address space that is mapped and accessible to your process rather than the amount of memory it directly uses.

This includes:

  • Private pages (e.g. those written by the process)
  • Shared pages
  • Virtual address space corresponding to mmaped files
  • IO address space of peripherals memory mapped into processes - GPU memory being the common one, but also sometimes serial busses such as Infiniband, Firewire and probably Thunderbolt

It's the third of these that accounts for the bulk of the address space usage as it includes the read-only portions of executing code, and in particular, shared libraries. The operating system will keep some of these resident in physical memory, but may jettison them when necessary (they can always be reloaded from disc as required).

Swapping is the process of writing the dirty pages belonging to a process to disc. There is no swapping in iOS. Instead iOS asks applications to reduce their memory consumption by deleting unused view controllers and flushing caches. If this doesn't solve the problem, it looks for processes to kill. Processes using lots of physical memory are prime candidates.

Question 2:

Wired memory is address space that must always be mapped to physical memory. Things that reside in wired memory are:

  • The operating system itself (some operating systems - such as the Windows NT Kernel - can actually swap parts of themselves out, but Darwin doesn't)
  • Private data used by the operating system
  • IO Buffers used for DMA
  • Any memory shared with the GPU.

I'm not quite sure of the distinction between active and inactive here, but in general, file-cache (e.g. mmaped pages) are always the first candidate for reallocation when there is pressure on memory. In this context, a free page is one the operating system is not using for anything. All decent modern operating systems will always use most of its free pages for file-cache. A page is generally only free because nothing yet has been cached in it.

Before you think that all of these pages are yours for the taking, remember that a lot of them are filled with application code. If that code is needed, it will be reloaded from disk.

Question 3:

The sum is inconsistent because of sharing of between processes. This is why shared libraries save memory. The Darwin kernel relies on a neat optimisation where child processes inherit memory mapped files from their parents. A top-level parent process - probably launchd mmaps a large number of common system libraries, thus ensuring child processes get them for free. This accounts for a large amount of virtual address space in each process, and explains why they are fairly similar at ~600MB.

Question 4:

You can't reliably. Your only option is to do it empirically. In other words, how much memory can you consume without your application causing performance of the rest of the system to suck, or getting terminated by the operating system for using too many resources. You can't even rely on the free pages being available to use more than short-term.

like image 138
marko Avatar answered Nov 23 '22 23:11

marko