Solved

Get amount of memory used by app in iOS

Posted on 2012-03-30
3
1,241 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Apple's line of laptop computers has made significant strides forward in the last few years. The biggest change being the switch to Intel processors in 2005. One thing about Apple hardware, especially that from a few years ago, is the tendency for i…
A lot of new and distinct gadgets are making their appearance every other day. The latest gadget that has wooed the attention of all gadget lovers and non gadget lovers alike is the Smartwatch. This tiny gadget is capable of offering live access to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now