iOS 内存分配 - 一个应用程序可以使用多少内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6044147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS memory allocation - how much memory can be used in an application?
提问by senthilMuthu
Does iOs use non-contiguous or contiguous allocation in memory management? suppose if user allocates more than 128 MB, Will the App be closed? or Memory will be managed by iOS as if user allocates memory and misses deallocate in Deallocate method? is it possible to use more than 120 MB in application using well-defined data structure allocation?
iOs 在内存管理中使用非连续分配还是连续分配?假设如果用户分配超过 128 MB,应用程序会被关闭吗?或者内存将由 iOS 管理,就好像用户分配内存并在解除分配方法中错过解除分配一样?是否可以使用定义明确的数据结构分配在应用程序中使用超过 120 MB?
回答by Skyler Saleh
Blocks from separate memory allocations are not allocated contiguously (separate calls to alloc, malloc, new, etc.). Otherwise they are allocated contiguously(from the same call to malloc, ex. new float[30]). According to Apple your app risks being shut down for memory usage when you use more than 20mb of ram. In practice however, you can get to about...
来自单独内存分配的块不是连续分配的(对 alloc、malloc、new 等的单独调用)。否则它们是连续分配的(来自对 malloc 的相同调用,例如 new float[30])。根据 Apple 的说法,当您使用超过 20mb 的内存时,您的应用程序可能会因内存使用而被关闭。然而在实践中,你可以得到约...
- 260 MB of ram on iPad 2 (Thanks RobCroll)
- 170-180MB of ram on devices with 512 Mb of ram total (iPhone 4, iPod touch 4g)
- 40-80MB of ram on devices that have 256 MB of ram (iPad, iPhone 3gs, iPod touch 3g)
- 25 MB on device with only 128MB of ram (IPhone 3g, iPhone 2g, iPod touch 1g-2g)
- iPad 2 上的 260 MB 内存(感谢 RobCroll)
- 170-180MB 内存,总内存为 512 Mb 的设备(iPhone 4、iPod touch 4g)
- 具有 256 MB 内存的设备(iPad、iPhone 3gs、iPod touch 3g)上的内存为 40-80MB
- 仅 128MB 内存的设备上 25 MB(iPhone 3g、iPhone 2g、iPod touch 1g-2g)
If you really "need" that much ram for a mobile application, you should really save the data to a temp file and do your processing on that. An easy way to do that is by using memory mapped files.
如果您真的“需要”移动应用程序的那么多内存,您真的应该将数据保存到临时文件中并对其进行处理。一种简单的方法是使用内存映射文件。
回答by Dipen Chudasama
You can use memory < your device ram capacity
您可以使用内存<设备的RAM容量
(e.g. you're using 40 Mb of RAM, and then allocating 80 Mb's more for some short computation). In this case iOS would kill your application immediately.1
(例如,您正在使用 40 Mb 的 RAM,然后分配 80 Mb 的更多用于一些短时间计算)。在这种情况下,iOS 会立即终止您的应用程序。1
List of results found by users testing with this tool:
用户使用此工具进行测试时发现的结果列表:
device: (crash amount/total amount/percentage of total)2
- 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 Pro 12.9: 3064MB/3981MB/77% (iOS 9.3.2)
- iPad Pro 9.7": 1395MB/1971MB/71% (iOS 10.0.2 (14A456))
- 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+: 1195MB/2048MB/58% (theoretical, untested)
- iPhoneSE: 1395MB/2048MB/69% (iOS 9.3)
- iPhone 6s+: 1392MB/2048MB/ 68% (iOS 10.2.1)
- iPhone 7+: 2040MB/3072MB/66% (iOS 10.2.1)
- iPhone X: 1392/2785/50% (iOS 11.2.1)
设备:(崩溃量/总量/总量百分比)2
- iPad1:127MB/256MB/49%
- iPad2:275MB/512MB/53%
- iPad3:645MB/1024MB/62%
- iPad4:585MB/1024MB/57% (iOS 8.1)
- iPad Mini 第一代:297MB/512MB/58%
- iPad Mini 视网膜:696MB/1024MB/68% (iOS 7.1)
- iPad Air:697MB/1024MB/68%
- iPad Air 2:1195MB/2048MB/58% (iOS 8.x)
- iPad Pro 12.9:3064MB/3981MB/77% (iOS 9.3.2)
- iPad Pro 9.7":1395MB/1971MB/71% (iOS 10.0.2 (14A456))
- iPod touch 第 4 代:130MB/256MB/51% (iOS 6.1.1)
- iPod touch 第 5 代: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+:1195MB/2048MB/58%(理论值,未经测试)
- iPhoneSE:1395MB/2048MB/69% (iOS 9.3)
- iPhone 6s+:1392MB/2048MB/68% (iOS 10.2.1)
- iPhone 7+:2040MB/3072MB/66% (iOS 10.2.1)
- iPhone X:1392/2785/50%(iOS 11.2.1)
1https://stackoverflow.com/a/5887783/5181636
1 https://stackoverflow.com/a/5887783/5181636
2https://stackoverflow.com/a/15200855/5181636
2 https://stackoverflow.com/a/15200855/5181636
More information can be found on this question.
可以在这个问题上找到更多信息。
回答by Giuliano Galea
Under the hood iOS uses malloc and friends to allocate memory for every object, so yes the memory returned is indeed contiguous. If you try to allocate more than available contiguous memory the malloc call will return NULL (and probably something else will fail when trying to access a null pointer if not properly checked)
在底层 iOS 使用 malloc 和朋友为每个对象分配内存,所以是的,返回的内存确实是连续的。如果您尝试分配的内存超过可用的连续内存,则 malloc 调用将返回 NULL(如果未正确检查,则在尝试访问空指针时可能还有其他事情会失败)
回答by Evgeny Karpov
Currently memory management in iOS works without clear memory limit for an application. But you can handle the situation when iOS tells your app to release memory immediately or it will be closed.
目前 iOS 中的内存管理工作没有明确的应用程序内存限制。但是当 iOS 告诉您的应用程序立即释放内存或将关闭时,您可以处理这种情况。
Responding to Low-Memory Warnings in iOS
128MB is quite a big memory block for iOS. in case if you will try to allocate over memory limit an application will be closed without any notifications.
128MB 对于 iOS 来说是一个相当大的内存块。如果您尝试分配超过内存限制,应用程序将在没有任何通知的情况下关闭。
回答by Krishnan
I dont know if app Memory usage limit is 128 MB or not. But if you consume more memory your application would receive memory warnings. If you handle them and clear cache and other objects that you can create at a later time, your application would not quit. If you ignore them, your application would quit.
我不知道应用内存使用限制是否为 128 MB。但是,如果您消耗更多内存,您的应用程序将收到内存警告。如果您处理它们并清除缓存和稍后可以创建的其他对象,您的应用程序将不会退出。如果您忽略它们,您的应用程序将退出。