Memory on the iPhone

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 16:45 点击:

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 should not use more than 46MB 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() calls do report something around 128MB 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 719MB. When I run it through Instruments the whole devices freezes at around 46MB. 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 across 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.


摘自 volcan1987的专栏

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Lexical or Preprocessor Issue 'xxx.h
      • ios学习笔记(二)xcode 4.3.2下实现基本交互
      • ios版本的helloworld
      • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
      • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
      • IOS类似iphone通讯录TableView的完整demo【附源码】
      • UITableView一些方法
      • [iPhone中级]iPhone团购信息客户端的开发 (二)
      • iphone(object-c)内存管理(1)
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1