Activity Monitor faithfully reflects the real-time changes in memory occupied by the program. However, in this mode, I don't think I can see the logs printed on the console. In short, there is no problem with the above method of viewing program memory,
However, if you want to print out the memory occupied by the program in the console, you can also use the following method:
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)
{
// Printf ("Memory vm: % u \ n", info. virtual_size );
// Printf ("Memory in use (in bytes): % u B \ n", info. resident_size );
// Printf ("Memory in use (in k-bytes): % f k \ n", info. resident_size/1024.0 );
Printf ("Memory in use (in m-bytes): % f m \ n", info. resident_size/(1024.0*1024.0 ));
}
Else
{
Printf ("Error with task_info (): % s \ n", mach_error_string (kerr ));
}
}
Remember to introduce the header file:
# Import <mach/mach. h>
If you want to directly call this method in the entire program, you can introduce the declaration in the *-Prefix. pch file:
# Ifdef _ OBJC __
# Import <UIKit/UIKit. h>
# Import <Foundation/Foundation. h>
# Endif
Extern void report_memory (void );
You can directly call this method where you want to print the memory:
-(Void) viewDidLoad
{
[Super viewDidLoad];
//// Your logic code ....
Report_memory ();
}
Below is the memory printed in the console. (From simulator)