Authored by 刘金林

内存监控 初版

... ... @@ -89,5 +89,5 @@ Pod::Spec.new do |s|
ss.dependency "YHEventReport/Core"
s.framework = "UIKit"
ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_DeviceInfo/*.{h,m}'
end
end
... ...
... ... @@ -40,16 +40,11 @@ static YH_DeviceInfoMonitor *yh_globalDeviceInfoMonitor = nil;
- (instancetype)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
return self;
}
- (void)willEnterForeground {
[self reportMemoryStatusIfNeeded];
}
- (void)didEnterBackground {
[self reportMemoryStatusIfNeeded];
}
... ... @@ -65,6 +60,9 @@ static YH_DeviceInfoMonitor *yh_globalDeviceInfoMonitor = nil;
@"iDeviceUsedMemory" : @(allUsedMemory),
@"iDeviceAvailableMemory" : @(availableMemory),
};
#ifdef DEBUG
NSLog(@"memory status:%@", dict);
#endif
[[YHEventReport sharedInstance] reportWithPointType:YHPT_PERFORMANCE pointName:YHPH_MEMORY parameters:dict];
}
... ... @@ -89,29 +87,22 @@ static YH_DeviceInfoMonitor *yh_globalDeviceInfoMonitor = nil;
}
- (NSUInteger)allUsedMemory {
size_t length = 0;
int mib[6] = {0};
int pagesize = 0;
mib[0] = CTL_HW;
mib[1] = HW_PAGESIZE;
length = sizeof(pagesize);
if (sysctl(mib, 2, &pagesize, &length, NULL, 0) < 0) {
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 page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;
kern = host_page_size(host_port, &page_size);
if (kern != KERN_SUCCESS) {
NSParameterAssert(NO);
return 0;
}
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
vm_statistics_data_t vmstat;
if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count) != KERN_SUCCESS) {
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
if (kern != KERN_SUCCESS) {
NSParameterAssert(NO);
return 0;
}
int wireMem = vmstat.wire_count * pagesize;
int activeMem = vmstat.active_count * pagesize;
return wireMem + activeMem;
return page_size * (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count);
}
- (NSUInteger)availableMemory {
... ...