Solved

Get amount of memory used by app in iOS

Posted on 2012-03-30
3
1,304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Apple Developer Reference describes NSInvocation this way: An NSInvocation is an Objective-C message rendered static, that is, it is an action tur…
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…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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