IOS study note 11-memory occupied by the print program in the code

Source: Internet
Author: User

 

 


 

 

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)

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.