Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Unified Diff: ios/chrome/app/application_delegate/memory_warning_helper.mm

Issue 1989873002: Create Memory Helper for memory warnings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add OWNERS Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/chrome/app/application_delegate/memory_warning_helper.mm
diff --git a/ios/chrome/app/application_delegate/memory_warning_helper.mm b/ios/chrome/app/application_delegate/memory_warning_helper.mm
new file mode 100644
index 0000000000000000000000000000000000000000..8f47c367c8cbb6726d826933695334a7e27d9b8c
--- /dev/null
+++ b/ios/chrome/app/application_delegate/memory_warning_helper.mm
@@ -0,0 +1,68 @@
+// 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_warning_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 MemoryWarningHelper () {
+ // 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)resetOutOfMemoryFlagIfNecessary;
+@end
+
+@implementation MemoryWarningHelper
+
+@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;
+
+ // Add information to breakpad.
+ breakpad_helper::SetMemoryWarningCount(_foregroundMemoryWarningCount);
+ breakpad_helper::SetMemoryWarningInProgress(true);
+
+ dispatch_after(kOutOfMemoryResetTimeInterval, dispatch_get_main_queue(), ^{
+ [self resetOutOfMemoryFlagIfNecessary];
+ });
+}
+
+- (void)resetOutOfMemoryFlagIfNecessary {
+ if (CFAbsoluteTimeGetCurrent() < _outOfMemoryResetTime)
+ return;
+ _outOfMemoryResetTime = 0;
+ [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag];
+ breakpad_helper::SetMemoryWarningInProgress(false);
+}
+
+- (void)resetForegroundMemoryWarningCount {
+ _foregroundMemoryWarningCount = 0;
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698