Programing

iOS 앱 최대 메모리 예산

lottogame 2020. 6. 15. 08:20
반응형

iOS 앱 최대 메모리 예산


최소 3gs를 목표로하는 iOS 게임을 만들고 있습니다. 우리는 레티 나 디스플레이 장치 (iphone 4, ipod touch 4 세대)에 HD 자산을 사용하고 있습니다.

메모리 측면에서 볼 때, Ipod Touch 4 세대는 3gs와 동일한 양의 RAM (256과 비교하여 256)을 갖기 때문에 가장 제약적인 장치 인 것 같습니다. 그러나 HD 자산을 사용하고 있습니다. 100-110mb의 램을로드하려고 할 때 응용 프로그램이 충돌했지만 이제는 70MB로 줄어 들었으므로로드가 발생하지 않았습니다.

주변을 많이 검색 한 후에는 공식적인 하드 제한이없는 것처럼 보이므로 안전을 위해 사용할 메모리 예산을 어떻게 알 수 있습니까? 아티스트가 각지도에 대한 메모리 걱정없이 사용할 수있는 예산을 제공 할 수 있기를 원합니다.


나는 당신이 당신의 자신의 질문에 대답했다고 생각합니다 : 70 Mb 한도를 넘지 마십시오. 그러나 그것은 실제로 많은 것들에 달려 있습니다 : 당신이 사용하는 iOS 버전 (SDK가 아닌), 백그라운드에서 실행중인 응용 프로그램의 수, 정확한 메모리 당신은 등을 사용하고 있습니다

즉석 메모리 스플래쉬를 피하십시오 (예 : 40Mb의 RAM을 사용하고 약간의 짧은 계산을 위해 80Mb를 더 할당하는 경우). 이 경우 iOS는 즉시 응용 프로그램을 종료합니다.

자산의 게으른 로딩도 고려해야합니다 (사전 필요하지 않은 경우에만로드).


유틸리티 Split으로 작성한 테스트 결과 (링크는 그의 답변입니다) :

기기 : (충돌 량 / 총량 / 총 백분율)

  • iPad1 : 127MB / 256MB / 49 %
  • iPad2 : 275MB / 512MB / 53 %
  • iPad3 : 645MB / 1024MB / 62 %
  • iPad4 : 585MB / 1024MB / 57 % (iOS 8.1)
  • iPad Mini 1 세대 : 297MB / 512MB / 58 %
  • iPad Mini 망막 : 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 베타 4)
  • iPad Pro 12.9”(2015) : 3058 / 3999 / 76 % (iOS 11.2.1)
  • iPad Pro 12.9”(2017) : 3057 / 3974 / 77 % (iOS 11 베타 4)
  • iPad Pro 11.0”(2018) : 2858 / 3769 / 76 % (iOS 12.1)
  • iPad Pro 12.9”(2018, 1TB) : 4598 / 5650 / 81 % (iOS 12.1)
  • iPod touch 4 세대 : 130MB / 256MB / 51 % (iOS 6.1.1)
  • iPod touch 5 세대 : 286MB / 512MB / 56 % (iOS 7.0)
  • 아이폰 4 : 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)
  • 아이폰 8 : 1364 / 1990MB / 70 % (iOS 12.1)
  • iPhone X : 1392 / 2785 / 50 % (iOS 11.2.1)
  • iPhone XS : 2040 / 3754 / 54 % (iOS 12.1)
  • iPhone XS Max : 2039 / 3735 / 55 % (iOS 12.1)
  • 아이폰 XR : 1792 / 2813 / 63 % (iOS 12.1)

나는 가능한 많은 메모리를 할당하려고 시도하는 작은 유틸리티를 만들었고 메모리 경고와 충돌이 발생했을 때 기록합니다. 이것은 iOS 기기의 메모리 예산을 찾는 데 도움이됩니다.

https://github.com/Split82/iOSMemoryBudgetTest


내 응용 프로그램에서 더 많은 메모리를 사용하면 사용자 경험이 향상되므로 가능한 모든 메모리 를 확보 해야하는지 결정해야합니다 didReceiveMemoryWarning. Split과 Jasper Pol의 답변에 따르면 총 장치 메모리의 최대 45 %를 사용하는 것이 안전한 임계 값 인 것 같습니다 (감사합니다).

누군가 내 실제 구현을보고 싶어하는 경우 :

#import "mach/mach.h"

- (void)didReceiveMemoryWarning
{
    // Remember to call super
    [super didReceiveMemoryWarning];

    // If we are using more than 45% of the memory, free even important resources,
    // because the app might be killed by the OS if we don't
    if ([self __getMemoryUsedPer1] > 0.45)
    {
        // Free important resources here
    }

    // Free regular unimportant resources always here
}

- (float)__getMemoryUsedPer1
{
    struct mach_task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &size);
    if (kerr == KERN_SUCCESS)
    {
        float used_bytes = info.resident_size;
        float total_bytes = [NSProcessInfo processInfo].physicalMemory;
        //NSLog(@"Used: %f MB out of %f MB (%f%%)", used_bytes / 1024.0f / 1024.0f, total_bytes / 1024.0f / 1024.0f, used_bytes * 100.0f / total_bytes);
        return used_bytes / total_bytes;
    }
    return 1;
}

Swift (based on this answer):

func __getMemoryUsedPer1() -> Float
{
    let MACH_TASK_BASIC_INFO_COUNT = (sizeof(mach_task_basic_info_data_t) / sizeof(natural_t))
    let name = mach_task_self_
    let flavor = task_flavor_t(MACH_TASK_BASIC_INFO)
    var size = mach_msg_type_number_t(MACH_TASK_BASIC_INFO_COUNT)
    var infoPointer = UnsafeMutablePointer<mach_task_basic_info>.alloc(1)
    let kerr = task_info(name, flavor, UnsafeMutablePointer(infoPointer), &size)
    let info = infoPointer.move()
    infoPointer.dealloc(1)
    if kerr == KERN_SUCCESS
    {
        var used_bytes: Float = Float(info.resident_size)
        var total_bytes: Float = Float(NSProcessInfo.processInfo().physicalMemory)
        println("Used: \(used_bytes / 1024.0 / 1024.0) MB out of \(total_bytes / 1024.0 / 1024.0) MB (\(used_bytes * 100.0 / total_bytes)%%)")
        return used_bytes / total_bytes
    }
    return 1
}

By forking SPLITS repo, I built one to test iOS memory that can be allocated to the Today's Extension

iOSMemoryBudgetTestForExtension

Following is the result that i got in iPhone 5s

Memory Warning at 10 MB

App Crashed at 12 MB

By this means Apple is merely allowing any extensions to work with their full potential.


You should watch session 147 from the WWDC 2010 Session videos. It is "Advanced Performance Optimization on iPhone OS, part 2".
There is a lot of good advice on memory optimizations.

Some of the tips are:

  • Use nested NSAutoReleasePools to make sure your memory usage does not spike.
  • Use CGImageSource when creating thumbnails from large images.
  • Respond to low memory warnings.

- (float)__getMemoryUsedPer1
{
    struct mach_task_basic_info info;
    mach_msg_type_number_t size = MACH_TASK_BASIC_INFO;
    kern_return_t kerr = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &size);
    if (kerr == KERN_SUCCESS)
    {
        float used_bytes = info.resident_size;
        float total_bytes = [NSProcessInfo processInfo].physicalMemory;
        //NSLog(@"Used: %f MB out of %f MB (%f%%)", used_bytes / 1024.0f / 1024.0f, total_bytes / 1024.0f / 1024.0f, used_bytes * 100.0f / total_bytes);
        return used_bytes / total_bytes;
    }
    return 1;
}

If one will use TASK_BASIC_INFO_COUNT instead of MACH_TASK_BASIC_INFO, you will get

kerr == KERN_INVALID_ARGUMENT (4)


I created one more list by sorting Jaspers list by device RAM (I made my own tests with Split's tool and fixed some results - check my comments in Jaspers thread).

device RAM: percent range to crash

  • 256MB: 49% - 51%
  • 512MB: 53% - 63%
  • 1024MB: 57% - 68%
  • 2048MB: 68% - 69%
  • 3072MB: 66%
  • 4096MB: 77%
  • 6144MB: 81%

Special cases:

  • iPhone X (3072MB): 50%
  • iPhone XS/XS Max (4096MB): 55%
  • iPhone XR (3072MB): not tested (please post crash values in comments)

Device RAM can be read easily:

[NSProcessInfo processInfo].physicalMemory

From my experience it is safe to use 45% for 1GB devices, 50% for 2/3GB devices and 55% for 4GB devices. Percent for macOS can be a bit bigger.

참고URL : https://stackoverflow.com/questions/5887248/ios-app-maximum-memory-budget

반응형