Possible Duplicate:
How to Programmatically Tell How Much Memory an iOS App is Using?可能重复:如何以编程方式判断iOS应用程序使用了多少内存?
I need to know how much memory is used by an iPhone an application when the application is running in foreground or background. it will better if it shows memory useage in every 5 sec. is it possible to write a code to show the memory in use ? Any suggestion will be appriciated
当应用程序在前台或后台运行时,我需要知道iPhone应用程序使用了多少内存。如果它每5秒显示一次内存使用情况会更好。是否可以编写代码来显示正在使用的内存?任何建议都会受到批评
2 个解决方案
#1
4
First include the method report_memory in the .h file then import
首先在.h文件中包含方法report_memory然后导入
#import <mach/mach.h>
this to the .m file
这到.m文件
After that write this line where you want to print the memory usage value
之后,在此行中写入要打印内存使用值的行
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(report_memory) userInfo:nil repeats:YES];
then add this
然后加上这个
-(void) report_memory {
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);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory usage: %.4lf MB", info.resident_size/1024.0/1024.0);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
method to .m file
.m文件的方法
#2
2
User NSTimer scheduled to run in 5 sec intervals. To get value of used memory, here you have some code
用户NSTimer计划以5秒为间隔运行。要获得已用内存的价值,这里有一些代码
#import <mach/mach.h>
void report_memory(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);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
#1
4
First include the method report_memory in the .h file then import
首先在.h文件中包含方法report_memory然后导入
#import <mach/mach.h>
this to the .m file
这到.m文件
After that write this line where you want to print the memory usage value
之后,在此行中写入要打印内存使用值的行
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(report_memory) userInfo:nil repeats:YES];
then add this
然后加上这个
-(void) report_memory {
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);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory usage: %.4lf MB", info.resident_size/1024.0/1024.0);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
method to .m file
.m文件的方法
#2
2
User NSTimer scheduled to run in 5 sec intervals. To get value of used memory, here you have some code
用户NSTimer计划以5秒为间隔运行。要获得已用内存的价值,这里有一些代码
#import <mach/mach.h>
void report_memory(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);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}