Memory on the iPhone

Source: Internet
Author: User

Now here is the question: How much memory does the iPhone have? How much memory is OK for applications to use? Curious as I am I did some research and wrote a little test project. This got me some odd results.
According to what you find on the net the iPhone has a total memory size of 128 MB and your application shocould not use more than 46 MB of it. but what happens exactly when you try to obtain more?
So I wrote a little application that continuously allocates more and more memory. In fact it mallocs/frees the memory on every timer tick.
Now this is where it gets odd. while the sysctl () CILS do report something around 128 MB of ram I can malloc () way beyond this. in fact callingmalloc (700000000) does not fail at all! When I run the application on just the iPhone it will stop at around 719 MB. when I run it through Instruments the whole devices freezes at around 46 MB. this has been reproduced on 2.1 and 2.2 on different devices. www.2cto.com

-(Void) tick
{
Allocated = allocated + size;

If (allocatedPtr ){
Free (allocatedPtr );
}

AllocatedPtr = malloc (allocated );

If (! AllocatedPtr ){
NSLog (@ "out of memory at % ld", allocated );
...

Later I found someone who stumbled into ss the same thing.
Lazyweb: what is going on here?
You can download the test application here. But as a disclaimer: you run this code on your own risk!

Update:
So afterall this means the result of malloc () has a different semantic of what I expected. Adding the following piece of code makes the program behave and gives the expected result.

Long * p = (long *) allocatedPtr;
Long count = allocated/sizeof (long );
Long I;
For (I = 0; I <count; I ++ ){
* P ++ = 0x12345678;
}

So it turns out if you allocate (and use !) Around 46-50 MB in your iPhone application it will just get terminated.


From volcan1987

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.