Chromium Code Reviews| Index: ios/chrome/app/application_delegate/memory_recovery_helper.mm |
| diff --git a/ios/chrome/app/application_delegate/memory_recovery_helper.mm b/ios/chrome/app/application_delegate/memory_recovery_helper.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5b76bff2bc347d5ff9aab28727949611ba941128 |
| --- /dev/null |
| +++ b/ios/chrome/app/application_delegate/memory_recovery_helper.mm |
| @@ -0,0 +1,66 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/app/application_delegate/memory_recovery_helper.h" |
| + |
| +#include "base/memory/memory_pressure_listener.h" |
| +#include "base/metrics/histogram.h" |
| +#include "ios/chrome/browser/crash_report/breakpad_helper.h" |
| +#import "ios/chrome/browser/metrics/previous_session_info.h" |
| + |
| +namespace { |
| +// The number of seconds to wait after a memory warning to clear the flag used |
| +// to detect Out Of Memory crashes. |
| +// NOTE: From local tests on various devices, this interval ranges between 1 and |
| +// 3 seconds. It is set to 5 to ensure all out of memory crashes are identified, |
| +// even if this may lead to overcounting them. |
| +const CFTimeInterval kOutOfMemoryResetTimeInterval = 5; |
| +} |
| + |
| +@interface MemoryRecoveryHelper () { |
| + // The time at which to reset the OOM crash flag in the user defaults. This |
| + // is used to handle receiving multiple memory warnings in short |
| + // succession. |
| + CFAbsoluteTime _outOfMemoryResetTime; |
| +} |
| +// Resets the OOM crash flag from the user defaults. |
| +- (void)resetOutOfMemoryFlag; |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:07
s/resetOutOfMemoryFlag/resetOutOfMemoryFlagIfNeces
gambard
2016/05/19 09:35:03
Done.
|
| +@end |
| + |
| +@implementation MemoryRecoveryHelper |
| + |
| +@synthesize foregroundMemoryWarningCount = _foregroundMemoryWarningCount; |
| + |
| +- (void)handleMemoryPressure { |
| + // Notify the system that the memory is critical and something should be done. |
| + base::MemoryPressureListener::NotifyMemoryPressure( |
| + base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| + |
| + ++_foregroundMemoryWarningCount; |
| + // Register that we might die because of memory. If we are still alive in |
| + // |kOutOfMemoryResetTimeInterval| seconds, reset the flag. |
| + [[PreviousSessionInfo sharedInstance] setMemoryWarningFlag]; |
| + _outOfMemoryResetTime = |
| + CFAbsoluteTimeGetCurrent() + kOutOfMemoryResetTimeInterval; |
| + [self performSelector:@selector(resetOutOfMemoryFlag) |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:07
Optional NIT: Consider using dispatch_after, which
gambard
2016/05/19 09:35:03
Done.
|
| + withObject:nil |
| + afterDelay:kOutOfMemoryResetTimeInterval]; |
| + // Add information to breakpad. |
| + breakpad_helper::SetMemoryWarningCount(_foregroundMemoryWarningCount); |
| + breakpad_helper::SetMemoryWarningInProgress(YES); |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:07
s/YES/true
gambard
2016/05/19 09:35:03
Done.
|
| +} |
| + |
| +- (void)resetOutOfMemoryFlag { |
| + if (CFAbsoluteTimeGetCurrent() < _outOfMemoryResetTime) |
| + return; |
| + _outOfMemoryResetTime = 0; |
| + [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag]; |
| + breakpad_helper::SetMemoryWarningInProgress(NO); |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:07
s/NO/false
gambard
2016/05/19 09:35:03
Done.
|
| +} |
| + |
| +- (void)resetForegroundMemoryWarningCount { |
| + _foregroundMemoryWarningCount = 0; |
| +} |
| + |
| +@end |