在 iOS 中查看内存使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7989864/
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
Watching memory usage in iOS
提问by Ron
Is there any way to find out how much memory is available in iOS? I know that the system will pass low memory warnings when available memory gets low. However, my App has some points where a single thread will perform a complex task and sometimes that task uses up enough memory that it is just terminated by the OS (my app can download pictures from the internet, and I scale them down to a small size ... if the user downloads a very large image, my app runs out of memory and just goes 'poof').
有没有办法找出iOS中有多少可用内存?我知道当可用内存变低时,系统会传递低内存警告。但是,我的应用程序有一些点,其中单个线程将执行复杂的任务,有时该任务会占用足够的内存,以至于它刚刚被操作系统终止(我的应用程序可以从互联网下载图片,我将它们缩小到一个小的大小......如果用户下载一个非常大的图像,我的应用程序内存不足,只会“噗”)。
Having the App spontaneously terminate is obviously a poor user experience.
让应用程序自发终止显然是一种糟糕的用户体验。
Is there any way that I can find out when I am about to run out of memory and stop the task instead?
有什么方法可以找出我即将耗尽内存并停止任务的时间?
I suppose I could put the task on a separate thread, and maybe the system would send the main thread a low memory warning, but that seems pretty complicated and not even guaranteed to work.
我想我可以把任务放在一个单独的线程上,也许系统会向主线程发送一个内存不足的警告,但这看起来很复杂,甚至不能保证工作。
Thanks! Ron
谢谢!罗恩
回答by progrmr
While testing and debugging your app with XCode you can use this logMemUsage()
function to NSLog the used/free space and watch how things are going while you test your app. This function logs any change in usage > 100kb. It outputs to the debug log like this (on the simulator the free space is huge):
在使用 XCode 测试和调试您的应用程序时,您可以使用此logMemUsage()
功能来 NSLog 已用/可用空间并在测试应用程序时观察事情的进展情况。此函数记录使用量 > 100kb 的任何变化。它像这样输出到调试日志(在模拟器上,可用空间很大):
2011-11-02 21:55:58.928 hello[971:207] Memory used 21884.9 (+21885), free 1838366.8 kb
2011-11-02 21:55:59.936 hello[971:207] Memory used 28512.3 (+6627), free 1830809.6 kb
2011-11-02 21:56:01.936 hello[971:207] Memory used 28803.1 ( +291), free 1830129.6 kb
2011-11-02 21:56:02.936 hello[971:207] Memory used 29712.4 ( +909), free 1830142.0 kb
You decide where to call logMemUsage
in your app. I happen to have a function that is called by a timer every second and so I put it in there. I suggest using #ifdef
around these so this code is only included in Debug builds.
您决定logMemUsage
在您的应用程序中调用的位置。我碰巧有一个每秒被计时器调用的函数,所以我把它放在那里。我建议#ifdef
围绕这些使用,以便此代码仅包含在调试版本中。
#import "mach/mach.h"
vm_size_t usedMemory(void) {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
return (kerr == KERN_SUCCESS) ? info.resident_size : 0; // size in bytes
}
vm_size_t freeMemory(void) {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
vm_size_t pagesize;
vm_statistics_data_t vm_stat;
host_page_size(host_port, &pagesize);
(void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
return vm_stat.free_count * pagesize;
}
void logMemUsage(void) {
// compute memory usage and log if different by >= 100k
static long prevMemUsage = 0;
long curMemUsage = usedMemory();
long memUsageDiff = curMemUsage - prevMemUsage;
if (memUsageDiff > 100000 || memUsageDiff < -100000) {
prevMemUsage = curMemUsage;
NSLog(@"Memory used %7.1f (%+5.0f), free %7.1f kb", curMemUsage/1000.0f, memUsageDiff/1000.0f, freeMemory()/1000.0f);
}
}
回答by Raptor
Actually each view controller has - (void)didReceiveMemoryWarning
functions.
实际上每个视图控制器都有- (void)didReceiveMemoryWarning
功能。
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
As suggested by the comments, you can release unused data under the comment. On the other hand, comment out [super didReceiveMemoryWarning];
to suppress memory warnings & auto release objects.
根据评论的建议,您可以在评论下释放未使用的数据。另一方面,注释掉[super didReceiveMemoryWarning];
以抑制内存警告和自动释放对象。
回答by featherless
I recommend checking out Nimbus' Overview tool for watching device statistics in real time. Out of the box it includes pages for viewing available memory, disk space and logs, as well as modifying log levels. It's also easy to add custom pages that show any information you want.
我建议查看 Nimbus 的概览工具以实时查看设备统计信息。开箱即用,它包括用于查看可用内存、磁盘空间和日志以及修改日志级别的页面。添加显示您想要的任何信息的自定义页面也很容易。
http://latest.docs.nimbuskit.info/NimbusOverview.html
http://latest.docs.nimbuskit.info/NimbusOverview.html
回答by Krishnabhadra
First the title of your question is how to watch memory usage in iOS..There is a tool called instrument comes with xcode, which you can use to track memory allocation, leaks, cpu usage and a host of other things..See apple's documentationon the subject..
首先,您的问题的标题是如何在 iOS 中查看内存使用情况..xcode 附带了一个名为仪器的工具,您可以使用它来跟踪内存分配、泄漏、cpu 使用情况和许多其他事情..请参阅苹果的文档就此主题而言..
- Now to see the real time memory usage of your app you can use allocator tool in instrument
- To identify the memory leaks you can use leak tool in instrument..
- 现在要查看您的应用程序的实时内存使用情况,您可以在仪器中使用分配器工具
- 要识别内存泄漏,您可以使用仪器中的泄漏工具..
Also in WWDC 2010 there is a videoof how to analyze memory using Instrument..
同样在 WWDC 2010 中有一个关于如何使用 Instrument 分析内存的视频。
回答by Robert Taylor
I love free code. Thank you progrmr, very useful. Time for me to start sharing back. I object-orientified it for my own use case.
我喜欢免费代码。谢谢programrmr,很有用。是时候开始分享了。我针对我自己的用例将它面向对象。
#import "mach/mach.h"
#import "memusage.h"
@implementation memusage
static long prevMemUsage = 0;
static long curMemUsage = 0;
static long memUsageDiff = 0;
static long curFreeMem = 0;
-(vm_size_t) freeMemory {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
vm_size_t pagesize;
vm_statistics_data_t vm_stat;
host_page_size(host_port, &pagesize);
(void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
return vm_stat.free_count * pagesize;
}
-(vm_size_t) usedMemory {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
return (kerr == KERN_SUCCESS) ? info.resident_size : 0; // size in bytes
}
-(void) captureMemUsage {
prevMemUsage = curMemUsage;
curMemUsage = [self usedMemory];
memUsageDiff = curMemUsage - prevMemUsage;
curFreeMem = [self freeMemory];
}
-(NSString*) captureMemUsageGetString{
return [self captureMemUsageGetString: @"Memory used %7.1f (%+5.0f), free %7.1f kb"];
}
-(NSString*) captureMemUsageGetString:(NSString*) formatstring {
[self captureMemUsage];
return [NSString stringWithFormat:formatstring,curMemUsage/1000.0f, memUsageDiff/1000.0f, curFreeMem/1000.0f];
}
@end
回答by Marc Brannan
The suite of development tools that apple provides includes "Instruments". You can use this to monitor allocations and leaks. In Xcode if you long click on the Run button you will see an option called "Profile". This will open up instruments automatically and allow you to select a profile to monitor your application.
苹果提供的开发工具套件包括“Instruments”。您可以使用它来监视分配和泄漏。在 Xcode 中,如果您长按“运行”按钮,您将看到一个名为“配置文件”的选项。这将自动打开仪器并允许您选择一个配置文件来监控您的应用程序。
回答by fabb
Sounds like you could use a well crafted library for the task of fetching web images. Nimbus has got a Network Image class which does that efficiently.
听起来您可以使用精心设计的库来执行获取网络图像的任务。Nimbus 有一个 Network Image 类,可以有效地做到这一点。