1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(@"------------------------memory warning");
[self mallocMemory];
NSLog(@"------------------------memory warningx");
}
- (IBAction)startMallocEvent:(id)sender
{
if (_timer.isValid) {
[_timer invalidate];
}
_timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(mallocMemory) userInfo:nil repeats:YES];
}
- (void)mallocMemory
{
const CGFloat KMemoryLength = 1024.0f * 1024.0f;
_pMemory[_count] = malloc(KMemoryLength);
memset(_pMemory[_count], 0, KMemoryLength);
++_count;
//实际内存
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t error = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
long curMemorySize = 0;
long curVirtualMemorySize = 0;
if (error == KERN_SUCCESS) {
curMemorySize = info.resident_size;
curVirtualMemorySize = info.virtual_size;
}
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);
long freeMemorySize = vm_stat.free_count * pagesize;
NSLog(@"curMemorySize = %5.1fM,VirtualMemorySize = %5.1fM,freeMemorySize = %5.1fM",
curMemorySize/KMemoryLength,
curVirtualMemorySize/KMemoryLength,
freeMemorySize/KMemoryLength);
uint64_t physicalMemorySize = 0;
uint64_t userMemorySize = 0;
int mib[2];
size_t length;
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
length = sizeof(int64_t);
sysctl(mib, 2, &physicalMemorySize, &length, NULL, 0);
mib[1] = HW_USERMEM;
length = sizeof(int64_t);
sysctl(mib, 2, &userMemorySize, &length, NULL, 0);
NSLog(@"physicalMemorySize = %5.1fM,userMemorySize = %5.1fM",
physicalMemorySize/KMemoryLength,
userMemorySize/KMemoryLength);
}
|