Solved

Get amount of memory used by app in iOS

Posted on 2012-03-30
3
1,251 Views
Last Modified: 2012-04-13
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 = 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
        }
    }

Open in new window


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];
    }

Open in new window


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.
0
Comment
Question by:HDM
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 37792823
Here is the code detecting the used memory:

#import <mach/mach.h> 
#import <mach/mach_host.h>

+ (NSUInteger)amountOfUsedMemory 
{
    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
    return (kerr == KERN_SUCCESS) ? info.resident_size : 0;
}

Open in new window


But I think, it's better to control the chunk size as you do it now.
0
 

Author Comment

by:HDM
ID: 37797839
Why would it be any better to use a static chunk size?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 37799475
If the file size that you need to upload is less than the predefined chunk size? So it's never a static size.
Why you took 20MB? Maybe this number can be much smaller? 4MB, for example?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

     Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON (https://github.com/SwiftyJSON/SwiftyJSON). If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
Thanks to luck and dedication (hitting refresh every 5 seconds when the pre-orders were going live at midnight on April 10th) I managed to actually receive an Apple Watch on release day, April 24th. Which means I’ve been using it daily for just ov…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question