app内存占用越来越大 ios
2018-09-17 14:26:00 u012109585 阅读数 75

获取 iOS APP 内存占用的大小

当我们想去获取 iOS 应用的占用内存时,通常我们能找到的方法是这样的,用 resident_size

 
#import <mach/mach.h>
- (int64_t)memoryUsage {
    int64_t memoryUsageInByte = 0;
    struct task_basic_info taskBasicInfo;
    mach_msg_type_number_t size = sizeof(taskBasicInfo);
    kern_return_t kernelReturn = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t) &taskBasicInfo, &size);
    
    if(kernelReturn == KERN_SUCCESS) {
        memoryUsageInByte = (int64_t) taskBasicInfo.resident_size;
        NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte);
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));
    }
    
    return memoryUsageInByte;
}

 

但是测试的时候,我们会发现这个跟我们在 Instruments 里面看到的内存大小不一样,有时候甚至差别很大。更加准确的方式应该是用 phys_footprint

 
#import <mach/mach.h>
- (int64_t)memoryUsage {
    int64_t memoryUsageInByte = 0;
    task_vm_info_data_t vmInfo;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
    kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &vmInfo, &count);
    if(kernelReturn == KERN_SUCCESS) {
        memoryUsageInByte = (int64_t) vmInfo.phys_footprint;
        NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte);
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));
    }
    return memoryUsageInByte;

 

关于 phys_footprint 的定义可以在 XNU 源码中,找到 osfmk/kern/task.c 里对于 phys_footprint 的注释:

 
/*
 * phys_footprint
 *   Physical footprint: This is the sum of:
 *     + (internal - alternate_accounting)
 *     + (internal_compressed - alternate_accounting_compressed)
 *     + iokit_mapped
 *     + purgeable_nonvolatile
 *     + purgeable_nonvolatile_compressed
 *     + page_table
 *
 * internal
 *   The task's anonymous memory, which on iOS is always resident.
 *
 * internal_compressed
 *   Amount of this task's internal memory which is held by the compressor.
 *   Such memory is no longer actually resident for the task [i.e., resident in its pmap],
 *   and could be either decompressed back into memory, or paged out to storage, depending
 *   on our implementation.
 *
 * iokit_mapped
 *   IOKit mappings: The total size of all IOKit mappings in this task, regardless of
     clean/dirty or internal/external state].
 *
 * alternate_accounting
 *   The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
 *   are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
 *   double counting.
 */

 

注释里提到的公式计算的应该才是应用实际使用的物理内存。

转载自新浪博客

posted on 2018-09-17 14:26 CodeVector 阅读(...) 评论(...) 编辑 收藏

2017-07-08 17:29:28 fishmai 阅读数 6316

http://stackoverflow.com/questions/5887248/iOS-app-maximum-memory-budget

device: (crash amount/total amount/percentage of total)

  • iPad1: 127MB/256MB/49%
  • iPad2: 275MB/512MB/53%
  • iPad3: 645MB/1024MB/62%
  • iPad4: 585MB/1024MB/57% (iOS 8.1)
  • iPad Mini 1st Generation: 297MB/512MB/58%
  • iPad Mini retina: 696MB/1024MB/68% (iOS 7.1)
  • iPad Air: 697MB/1024MB/68%
  • iPad Air 2: 1195MB/2048MB/58% (iOS 8.x)
  • iPad Air 2: 1383MB/2048MB/68% (iOS 10.2.1)
  • iPad Pro 9.7": 1395MB/1971MB/71% (iOS 10.0.2 (14A456))
  • iPad Pro 12.9: 3064MB/3981MB/77% (iOS 9.3.2)
  • iPod touch 4th gen: 130MB/256MB/51% (iOS 6.1.1)
  • iPod touch 5th gen: 286MB/512MB/56% (iOS 7.0)
  • iPhone4: 325MB/512MB/63%
  • iPhone4s: 286MB/512MB/56%
  • iPhone5: 645MB/1024MB/62%
  • iPhone5s: 646MB/1024MB/63%
  • iPhone6: 645MB/1024MB/62% (iOS 8.x)
  • iPhone6+: 645MB/1024MB/62% (iOS 8.x)
  • iPhone6s: 1396MB/2048MB/68% (iOS 9.2)
  • iPhone6s+: 1392MB/2048MB/68% (iOS 10.2.1)
  • iPhoneSE: 1395MB/2048MB/69% (iOS 9.3)
  • iPhone7: 1395/2048MB/68% (iOS 10.2)。    
    最低存储容量为32G,另外两个容量是128G和256G.
  • iPhone7+: 2040MB/3072MB/66% (iOS 10.2.1)      别名:A1661
    存储有32GB/128GB以及256GB可选
2018-12-29 14:12:08 aizhiqiang2 阅读数 670

http://stackoverflow.com/questions/5887248/iOS-app-maximum-memory-budget

device: (crash amount/total amount/percentage of total)

iPad1: 127MB/256MB/49%
iPad2: 275MB/512MB/53%
iPad3: 645MB/1024MB/62%
iPad4: 585MB/1024MB/57% (iOS 8.1)
iPad Mini 1st Generation: 297MB/512MB/58%
iPad Mini retina: 696MB/1024MB/68% (iOS 7.1)
iPad Air: 697MB/1024MB/68%
iPad Air 2: 1195MB/2048MB/58% (iOS 8.x)
iPad Air 2: 1383MB/2048MB/68% (iOS 10.2.1)
iPad Pro 9.7": 1395MB/1971MB/71% (iOS 10.0.2 (14A456))
iPad Pro 12.9: 3064MB/3981MB/77% (iOS 9.3.2)
iPod touch 4th gen: 130MB/256MB/51% (iOS 6.1.1)
iPod touch 5th gen: 286MB/512MB/56% (iOS 7.0)
iPhone4: 325MB/512MB/63%
iPhone4s: 286MB/512MB/56%
iPhone5: 645MB/1024MB/62%
iPhone5s: 646MB/1024MB/63%
iPhone6: 645MB/1024MB/62% (iOS 8.x)
iPhone6+: 645MB/1024MB/62% (iOS 8.x)
iPhone6s: 1396MB/2048MB/68% (iOS 9.2)
iPhone6s+: 1392MB/2048MB/68% (iOS 10.2.1)
iPhoneSE: 1395MB/2048MB/69% (iOS 9.3)
iPhone7: 1395/2048MB/68% (iOS 10.2)。    
最低存储容量为32G,另外两个容量是128G和256G.
iPhone7+: 2040MB/3072MB/66% (iOS 10.2.1)      别名:A1661
存储有32GB/128GB以及256GB可选
--------------------- 
作者:麦晓宇 
来源:CSDN 
原文:https://blog.csdn.net/fishmai/article/details/74840514 
版权声明:本文为博主原创文章,转载请附上博文链接!

2018-11-23 11:17:00 weixin_33690963 阅读数 2

 

1、相信很多小伙伴都在app内用过地图模块 ,可能是第三方的(如:百度,高德等),但是第三方的地图不如原生的界面清晰度高,也没有原生的界面渲染的速度快,这也从侧面反应出原生的mapview占用内存高的一个原因了

2、相信很多的人都用app中发现,只有导航类的APP才会把内嵌mapview的Viewcontroller作为首页,其他的APP都不会这么做,当然导致这种现象的主要原因可以归结于业务需求导致,但是小不分原因是因为mapview的内存占用是在是太高,导致APP内剩余业务所能使用的内存急剧的减少,最后导致进入其他业务模块会出现闪退的情况。

3、言归正传,到底怎么才能优化mapview在APP中的内存占用情况呢

  • 相信很多人查到的情况都是类似于 先把mapview移除,然后再做添加 如下操作:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    [mapView removeFromSuperview];
    [self.view addSubview:mapView];
} 

 对于用这种方法,内存的占用情况如下图

 

也不能说这种操作无效,因为mapview在滑动的时候,确实有内存的减少,但是对于将mapview作为app的首页进行内嵌显示的时候,这种优化显得很鸡肋,而且明显站不住脚,要知道上述操作从理论上来讲 ,对内存释放没有什么帮助,因为Viewcontroller是作为底部菜单存在的,那就会延伸出来本文的中心论点,怎么才能优化地图对内存的占用呢,那就是增加一个改变地图类型的方法 ,强制地图对内容进行重绘,此时在因为地图内容占用的内存就会得到释放

 

  • 增加改变地图类型的方法,并在上述方法中进行调用
- (void)applyMapViewMemoryRelease{
   
    MKMapType _type = _mapView.mapType;
    switch (_mapView.mapType) {
        case MKMapTypeHybrid:
        {
            _mapView.mapType = MKMapTypeStandard;
        }
            
            break;
        case MKMapTypeStandard:
        {
            _mapView.mapType = MKMapTypeHybrid;
        }
            
            break;
        default:
            break;
    }
    _mapView.mapType = _type;
}

调用方式

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    [self applyMapViewMemoryRelease];
    [mapView removeFromSuperview];
    [self.view addSubview:mapView];
}

运行以后,测试内存的占用情况,如下图

但是这种方法会有一个额外的副作用,就是在滑动地图的时候,地图会先闪现空白的网格,然后才会加载地图,而且地图的加载速度依赖于网络速度,所以就会增加如下的优化建议,怎么在地图移动或者缩放到一定的级别以后 ,再修改地图的类型,那就需要对上述方法进行改造,如下:

- (void)applyMapViewMemoryRelease{
    //其中 mapRegionSpan 是一个全局变量
    if (self.mapView.region.span.longitudeDelta < 0.005 || self.mapView.region.span.latitudeDelta < 0.005) {
        return;
    }
    
    float longitudeDelta = self.mapView.region.span.longitudeDelta - mapRegionSpan.longitudeDelta;
    float latitudeDelta = self.mapView.region.span.latitudeDelta - mapRegionSpan.latitudeDelta;
    
    if ( fabs(longitudeDelta) < 0.00005 && fabs(latitudeDelta) <0.00005) {
        return;
    }
    
    MKMapType _type = _mapView.mapType;
    switch (_mapView.mapType) {
        case MKMapTypeHybrid:
        {
            _mapView.mapType = MKMapTypeStandard;
        }
            
            break;
        case MKMapTypeStandard:
        {
            _mapView.mapType = MKMapTypeHybrid;
        }
            
            break;
        default:
            break;
    }
    _mapView.mapType = _type;
    mapRegionSpan = self.mapView.region.span;
}

用以上方法就可以解决用户在滑动app,或者只是碰到地图以后 ,不会执行此方法的逻辑,也就不会显示网格了

 

转载于:https://www.cnblogs.com/tianlin106/p/10006381.html

2018-06-15 11:20:00 weixin_34143774 阅读数 55

https://stackoverflow.com/questions/5887248/iOS-app-maximum-memory-budget

device: (crash amount/total amount/percentage of total)

iPad1: 127MB/256MB/49%
iPad2: 275MB/512MB/53%
iPad3: 645MB/1024MB/62%
iPad4: 585MB/1024MB/57% (iOS 8.1)
iPad Mini 1st Generation: 297MB/512MB/58%
iPad Mini retina: 696MB/1024MB/68% (iOS 7.1)
iPad Air: 697MB/1024MB/68%
iPad Air 2: 1383MB/2048MB/68% (iOS 10.2.1)
iPad Pro 9.7": 1395MB/1971MB/71% (iOS 10.0.2 (14A456))
iPad Pro 10.5”: 3057/4000/76% (iOS 11 beta4)
iPad Pro 12.9” (2015): 3058/3999/76% (iOS 11.2.1)
iPad Pro 12.9” (2017): 3057/3974/77% (iOS 11 beta4)
iPod touch 4th gen: 130MB/256MB/51% (iOS 6.1.1)
iPod touch 5th gen: 286MB/512MB/56% (iOS 7.0)
iPhone4: 325MB/512MB/63%
iPhone4s: 286MB/512MB/56%
iPhone5: 645MB/1024MB/62%
iPhone5s: 646MB/1024MB/63%
iPhone6: 645MB/1024MB/62% (iOS 8.x)
iPhone6+: 645MB/1024MB/62% (iOS 8.x)
iPhone6s: 1396MB/2048MB/68% (iOS 9.2)
iPhone6s+: 1392MB/2048MB/68% (iOS 10.2.1)
iPhoneSE: 1395MB/2048MB/69% (iOS 9.3)
iPhone7: 1395/2048MB/68% (iOS 10.2)
iPhone7+: 2040MB/3072MB/66% (iOS 10.2.1)
iPhone X: 1392/2785/50% (iOS 11.2.1)

没有更多推荐了,返回首页