简体   繁体   中英

Get amount of memory used by app in iOS

I'm working on an upload app that splits files before upload. It splits the files to prevent being closed by iOS for using too much memory as some of the files can be rather large. It would be great if I could, instead of setting the max "chunk" size, set the max memory usage and determine the size using that.

Something like this

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

-(void)uploadAsset:(ALAsset*)asset
{
    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
    }
}

Is essentially what I'm going for. I can't seem to find a way to get the current memory usage of my app specifically. I don't really care about the amount of system memory left.

The only way I've been able to think of is one I don't like much. Grab the amount of system memory on the first line of main in my app, then store it in a static variable in a globals class then the getCurrentMemUsage would go something like this

-(long)getCurrentMemUsage
{
    long sysUsage = [self getSystemMemoryUsed];
    return sysUsage - [Globals origSysUsage];
}

This has some serious drawbacks. The most obvious one to me is that another app might get killed in the middle of my upload, which could drop sysUsage lower than origSysUsage resulting in a negative number even if my app is using 10MB of memory which could result in my app using 40MB for a request rather than the maximum which is 20MB. I could always set it up so it clamps the value between MIN_CHUNK_SIZE and MAX_MEM_USAGE, but that would just be a workaround instead of an actual solution.

If there are any suggestions as to getting the amount of memory used by an app or even different methods for managing a dynamic chunk size I would appreciate either.

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

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

This function is really easy to use in iOS with the Cocoa APIs 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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM