Monitor live memory for Network Exension

Hi Eskimo,

I've succesfully implemented a DNSProxyProvider to intercept and relay the DNS requests on an iOS device. It runs without issues for a while, but I noticed than when out and in the wild the extension is terminated after a certain amount of time (around 3 hours at most). Obviously, I am not attached to Instruments when that happens, so my aim is to periodically log the memory usage of the extension to confirm my suspicion that the provider is terminated by the system as it exceeds the 15 Mib limit. I've read the various posts regarding memory limits and memory strategy when it comes to Netwrok Extensions. I was wondering if there is a way to retrieve programmatically the live (as opposed to resident) memory usage of the Extension process purely for debugging purposes. Any suggestion?

Accepted Reply

There’s no way to get the exact memory values returned by various Apple tools (Instruments, Activity Monitor,

vmmap
,
top
, and so on). These all do some amount of processing of the raw values, and the algorithms they use is both not documented and subject to change over time.

However, that does not prevent you from building some sort of analysis into your product. You just have to make sure you compare like with like, and not your analysis with, say, Instruments.

For a Network Extension provider you should take a look at

malloc_zone_statistics
. This counts all memory managed by
malloc
, which is where all the action is in a Network Extension provider (that is not true for an app, where you often see large allocations outside of malloc).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

There’s no way to get the exact memory values returned by various Apple tools (Instruments, Activity Monitor,

vmmap
,
top
, and so on). These all do some amount of processing of the raw values, and the algorithms they use is both not documented and subject to change over time.

However, that does not prevent you from building some sort of analysis into your product. You just have to make sure you compare like with like, and not your analysis with, say, Instruments.

For a Network Extension provider you should take a look at

malloc_zone_statistics
. This counts all memory managed by
malloc
, which is where all the action is in a Network Extension provider (that is not true for an app, where you often see large allocations outside of malloc).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Eskimo, always very helpful.

We use the task_vm_info.phys_footprint, as described by Eskimo here and found that it lines up very well with the actual 15MB ceiling imposed by iOS:


    struct task_vm_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&info, &size);
    if (kerr == KERN_SUCCESS) {
        vm_size_t used = (unsigned long)info.phys_footprint; // size in bytes
        return used;
    }