Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get amount of memory used by app in iOS

Posted on 2012-03-30
3
Medium Priority
?
1,328 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

What's a UDID? If you're involved in developing, testing, or even reviewing an iOS application that's in beta, then at some point you may need to know the UDID for any iOS devices that you'll be testing on. What's the UDID? It stands for Unique Dev…
     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…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

688 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