繁重的CPU处理加热设备

时间:2015-01-01 21:26:28

标签: ios objective-c iphone multithreading

我的公司已经创建了一个在现场使用的企业应用程序。

我们发现我们的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");
}

1 个答案:

答案 0 :(得分:2)

你有很多自动释放的对象,永远不会被释放。在外循环中添加自动释放池:

while (1 == 1) {
    @autoreleasepool {
        // original contents of your loop
    }
}