我的公司创建了一个在现场使用的企业应用程序。
我们发现我们的iPhone 5在0 * C以下无法正常工作。
我们希望尝试的一种解决方案是运行繁重的CPU密集型任务(相当于运行3D游戏),这将增加设备的热量(除了我们用来帮助他们的其他外部热量之外)。
使用室内激光温度计作为基准,当iPhone 5屏幕在应用程序主屏幕上闲置时,我在iPhone 5屏幕上测量了24 * C。 当我启动密集的3D游戏时,手机会发热,屏幕在几分钟内记录35 * C。
我们如何重新创建一个不消耗资源和使应用程序崩溃的密集型后台线程 CPU 进程? 我已经尝试了来自GitHub的各种开源iOS基准测试应用程序,但是当我将它们放在后台线程中的无限循环中时,几分钟后该应用程序会耗尽内存并崩溃。
这是我在无限循环中放入后台的基准测试之一的示例。(而 1=1)。 无论我尝试哪种基准代码变体,"总字节数"都会不停地增长,直到应用程序崩溃。点击查看仪器截图。
有没有人有我可以使用的 CPU 密集型代码示例,这些代码会使设备变得又好又热,还可以防止应用程序无限期地消耗资源?
电池消耗不是问题,因为我们将使用连接的外部电池组来抵消任何消耗。
所有的帮助感谢!
亚当
UIApplication * application = [UIApplication sharedApplication];
if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
NSLog(@"Multitasking Supported");
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
//To make the code block asynchronous
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//### background task starts
NSLog(@"Running in the backgroundn");
static NSString *staticString = @"some const value string to referece";
while (1 == 1)
{
NSDictionary *dictionary;
NSString *string;
NSDate *staticStart = [NSDate date];
NSLog(@"start timing");
int times = 10000000;
while (times--) {
static dispatch_once_t onceToken;
static NSDictionary *dict;
dispatch_once(&onceToken, ^{
dict = @{@"somekey":@"someotherlongvalue", @"someotherkey":@"someotherlongvalue", @"onelastkey":@"onelastvalue"};
});
dictionary = dict;
}
NSDate *staticEnd = [NSDate date];
NSLog(@"finished static dict in %f sec", [staticEnd timeIntervalSinceDate:staticStart]);
times = 10000000;
while (times--) {
dictionary = @{@"somekey":@"someotherlongvalue", @"someotherkey":@"someotherlongvalue", @"onelastkey":@"onelastvalue"};
}
NSDate *dictEnd = [NSDate date];
NSLog(@"finished dict create in %f sec", [dictEnd timeIntervalSinceDate:staticEnd]);
times = 10000000;
while (times--) {
static dispatch_once_t stringOnceToken;
static NSString *dispatchString;
dispatch_once(&stringOnceToken, ^{
dispatchString = @"someotherlongvalue";
});
string = dispatchString;
}
NSDate *staticStringEnd = [NSDate date];
NSLog(@"finished static string in %f sec", [staticStringEnd timeIntervalSinceDate:dictEnd]);
times = 10000000;
while (times--) {
string = @"someotherlongvalue";
}
NSDate *stringEnd = [NSDate date];
NSLog(@"finished string create in %f sec", [stringEnd timeIntervalSinceDate:staticStringEnd]);
times = 10000000;
while (times--) {
string = staticString;
}
NSDate *refEnd = [NSDate date];
NSLog(@"finished string reference in %f sec", [refEnd timeIntervalSinceDate:stringEnd]);
}
//#### background task ends
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
});
}
else
{
NSLog(@"Multitasking Not Supported");
}
你有很多自动释放的对象,这些对象从来没有机会被释放。将自动释放池添加到外部循环:
while (1 == 1) {
@autoreleasepool {
// original contents of your loop
}
}