ios 在应用程序运行时检查内存使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6438756/
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
Checking Memory Usage While Application is Running
提问by Ohad Regev
While running my app, I sometimes get Memory Warning. It doesn't always happen in same place in the code, but I suspect it has something to do with memory allocated from outside of my app; i.e. if there are many applications running on the iPhone in the background I will get the memory warning earlier, and if there are no applications running in the background I will not get the warning at all.
在运行我的应用程序时,我有时会收到内存警告。它并不总是发生在代码的同一个地方,但我怀疑它与从我的应用程序外部分配的内存有关;即如果后台有很多应用程序在 iPhone 上运行,我会更早收到内存警告,如果后台没有应用程序运行,我根本不会收到警告。
- Is there a method I can use to check how much memory my application is using at a certain point?
- Is there a method I can use to check how much memory the machine (iPhone/iPad) is using at a certain point? maybe even to check how much memory is still available to use?
- In general, maybe somebody knows where I can find data about the memory limitations for the different iOS based machines?
- 有没有一种方法可以用来检查我的应用程序在某个时刻使用了多少内存?
- 有没有我可以用它来检查多少内存的机器(iPhone / iPad的)在某一点使用方法?甚至可以检查还有多少内存可用?
- 一般来说,也许有人知道我在哪里可以找到有关基于 iOS 的不同机器的内存限制的数据?
Thanks, Ohad
谢谢,奥哈德
回答by Pieter
Using Instruments
you can check how much memory your app is using.
In Xcode4, use 'Profile' build, choose Leaks
, then click the Library button in the toolbar and add the Memory Monitor
instrument.
使用Instruments
您可以检查您的应用程序使用了多少内存。在 Xcode4 中,使用“Profile”构建,选择Leaks
,然后单击工具栏中的“库”按钮并添加Memory Monitor
仪器。
It will show an overview of every app that is running and how much memory each is using.
它将显示正在运行的每个应用程序的概述以及每个应用程序使用的内存量。
回答by sudo rm -rf
If you don't want to use Instruments, there is actually a utility class that Giulio Petek wrote that gets the current memory usage.
如果您不想使用 Instruments,实际上 Giulio Petek 编写了一个实用程序类来获取当前内存使用情况。
Try it out here: http://forrst.com/posts/Get_current_Memory_usage-hzw
回答by Say2Manuj
You can check the memory usage, using vm_statistics_data_t
object. Kindly find the details and implementation here:
您可以使用vm_statistics_data_t
对象检查内存使用情况。请在此处找到详细信息和实现:
回答by Paul Tiarks
You shouldn't worry about checking how much memory is available. The OS manages the memory and will issue warnings to the top memory consumers when memory is running low. What you need to do is make sure you're properly handling didReceiveMemoryWarning
messages and getting rid of any data that can be reloaded on demand later. Also, if your app is experiencing performance problems, you should use Instruments to check your apps usage in common situations and be sure you're using memory efficiently and only loading large objects that MUST be in memory at that time.
您不必担心检查有多少可用内存。操作系统管理内存,并在内存不足时向最高内存消费者发出警告。您需要做的是确保您正确处理didReceiveMemoryWarning
消息并清除以后可以按需重新加载的任何数据。此外,如果您的应用程序遇到性能问题,您应该使用 Instruments 来检查您的应用程序在常见情况下的使用情况,并确保您有效地使用内存并且只加载当时必须在内存中的大对象。