ios - 繁重的 CPU 处理以加热设备

标签 ios objective-c iphone multithreading

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

我们发现我们的 iPhone 5 在低于 0*C 的温度下无法正常工作。

我们正在尝试的一种解决方案是运行繁重的 CPU 密集型任务(相当于运行 3D 游戏),这会增加设备的热量(除了我们用来帮助​​它们的其他外部热量之外)。

在室内使用激光温度计作为基准,当 iPhone 5 在应用程序主屏幕上闲置时,我在 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");
}

最佳答案

您有很多自动释放的对象,它们从来没有机会被释放。将自动释放池添加到您的外循环:

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

关于ios - 繁重的 CPU 处理以加热设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734837/

相关文章:

iphone - 动画 NSLayoutConstraints 更改(以及对更改的对象有约束的对象)

ios - Swift 枚举不同类型的关联值

c++ - 在 iOS 应用程序中链接静态库后架构 arm64 的 undefined symbol

iphone - 如何从一个 UITextField 获取值到另一个 UITextField ?

ios - UIKeyboardWillShowNotification在iOS8中不触发

objective-c - 使用 QLPreviewController 水平滚动 PDF

iphone - 为什么我无法将 NSSet 保存到 coredata 中的 'to-many' 关系?

iphone - 带按钮的 UIscrollview 中的动画

iphone - 如何使用共享实例调用方法?

iphone - 训练 tesseract 与 iPhone 一起使用