Solved

Get amount of memory used by app in iOS

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
I recently went through the process of creating an installable image of an iPhone mobile app (an .ipa file) that beta testers could load onto their test devices. The process involved quite a few steps, but both Appcelerator and Diawi.org had decent …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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.

744 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

12 Experts available now in Live!

Get 1:1 Help Now