【发布时间】:2015-02-28 08:27:57
【问题描述】:
我的公司创建了一个在现场使用的企业应用程序。
我们发现我们的 iPhone 5 在 0*C 以下无法正常工作。
我们正在尝试尝试的一种解决方案是运行 CPU 密集型任务(相当于运行 3D 游戏),这会增加设备的热量(除了我们用来帮助它们的其他外部热量)。
使用室内激光温度计作为基准,我在 iPhone 5 屏幕上测量了 24*C,当它在应用程序主屏幕上空闲时。当我启动一个密集的 3D 游戏时,手机会发热,并且屏幕会在几分钟内注册 35*C。
我们如何重新创建一个密集的后台线程 CPU 进程而不消耗资源并使应用程序崩溃?我已经尝试了来自 GitHub 的各种开源 iOS 基准测试应用程序,但是当我将它们放在后台线程中的无限循环中时,几分钟后应用程序就会耗尽内存并崩溃。
这是我在无限循环中放入背景的基准之一的示例。 (而 1=1)。无论我尝试什么样的基准代码变体,“总字节数”都会不断增长,直到应用程序崩溃。 Click to see Instruments Screenshot.
有没有人有我可以使用的 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 background\n");
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");
}
【问题讨论】:
-
仅供参考 - 无需检查多任务处理。自 iOS 4.3 以来的所有设备都支持它。
-
1 == 1可以写成YES,更具可读性 -
我认为只执行繁重的计算任务会更有意义,例如重复执行矩阵乘法或类似的操作。
-
查看 ARM 或一般 CPU 基准测试以了解人们通常会做什么来保持 CPU 忙碌可能是有意义的。
标签: ios objective-c iphone multithreading