Get the amount of memory used by an app in iOS

I am working on a download application that splits files before uploading. It splits files to prevent iOS from closing for using too much memory as some of the files can be quite large. It would be great if I could, instead of setting the maximum chunk size, set the maximum memory usage and determine the size using that.

Something like that

#define MAX_MEM_USAGE 20000000 //20MB
#define MIN_CHUNK_SIZE 5000   //5KB

    long totalBytesRead = 0;
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    while(totalBytesRead < [representation size])
        long chunkSize = MAX_MEM_USAGE - [self getCurrentMemUsage];
        chunkSize = min([representation size] - totalBytesRead,max(chunkSize,MIN_CHUNK_SIZE));//if I can't get 5KB without getting killed then I'm going to get killed
        uint8_t *buffer = malloc(chunkSize);
        //read file chunk in here, adding the result to totalBytesRead
        //upload chunk here


I am essentially what I'm going to do. I cannot find a way to get the current memory usage in my application. I don't care about the amount of system memory remaining.

The only way I could think of is what I don't like. Grab the amount of system memory on the first line of the main one in my application and then store it in a static variable in the globals class, then getCurrentMemUsage will look something like this.

    long sysUsage = [self getSystemMemoryUsed];
    return sysUsage - [Globals origSysUsage];


This has some serious disadvantages. The most obvious to me is that another application might be killed in the middle of my boot, which might cause sysUsage to drop lower than origSysUsage, resulting in a negative number even though my application is using 10MB of memory, which could result in that my app is using 40MB for a request, not the maximum, which is 20MB. I could always configure it to clamp the value between MIN_CHUNK_SIZE and MAX_MEM_USAGE, but that would just be a workaround instead of a real solution.

If there are suggestions for getting the amount of memory used by the application, or even different methods for managing the dynamic chunk size, I would be grateful too.


source to share

1 answer

Now, as with any virtual memory operating system, getting "memory used" is not well defined and is notoriously difficult to define and calculate.

Fortunately, thanks to the virtual memory manager, your problem can be solved quite easily: mmap () . Basically, this allows your application to actually load the file into memory, treating it as if it were in RAM, but it actually swaps out storage as it is accessed, and swaps out when iOS is low in memory.

This feature is very easy to use on iOS with the Cocoa API for it:

- (void) uploadMyFile:(NSString*)fileName {
    NSData* fileData = [NSData dataWithContentsOfMappedFile:fileName];
    // Work with the data as with any NSData* object. The iOS kernel
    // will take care of loading the file as needed.




All Articles