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

Side by Side Diff: ios/chrome/app/application_delegate/memory_recovery_helper.mm

Issue 1989873002: Create Memory Helper for memory warnings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reviewable 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/app/application_delegate/memory_recovery_helper.h"
6
7 #include "base/memory/memory_pressure_listener.h"
8 #include "base/metrics/histogram.h"
9 #include "ios/chrome/browser/crash_report/breakpad_helper.h"
10 #import "ios/chrome/browser/metrics/previous_session_info.h"
11
12 namespace {
13 // The number of seconds to wait after a memory warning to clear the flag used
14 // to detect Out Of Memory crashes.
15 // NOTE: From local tests on various devices, this interval ranges between 1 and
16 // 3 seconds. It is set to 5 to ensure all out of memory crashes are identified,
17 // even if this may lead to overcounting them.
18 const CFTimeInterval kOutOfMemoryResetTimeInterval = 5;
19 }
20
21 @interface MemoryRecoveryHelper () {
22 // The time at which to reset the OOM crash flag in the user defaults. This
23 // is used to handle receiving multiple memory warnings in short
24 // succession.
25 CFAbsoluteTime _outOfMemoryResetTime;
26 }
27 // Resets the OOM crash flag from the user defaults.
28 - (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.
29 @end
30
31 @implementation MemoryRecoveryHelper
32
33 @synthesize foregroundMemoryWarningCount = _foregroundMemoryWarningCount;
34
35 - (void)handleMemoryPressure {
36 // Notify the system that the memory is critical and something should be done.
37 base::MemoryPressureListener::NotifyMemoryPressure(
38 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
39
40 ++_foregroundMemoryWarningCount;
41 // Register that we might die because of memory. If we are still alive in
42 // |kOutOfMemoryResetTimeInterval| seconds, reset the flag.
43 [[PreviousSessionInfo sharedInstance] setMemoryWarningFlag];
44 _outOfMemoryResetTime =
45 CFAbsoluteTimeGetCurrent() + kOutOfMemoryResetTimeInterval;
46 [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.
47 withObject:nil
48 afterDelay:kOutOfMemoryResetTimeInterval];
49 // Add information to breakpad.
50 breakpad_helper::SetMemoryWarningCount(_foregroundMemoryWarningCount);
51 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.
52 }
53
54 - (void)resetOutOfMemoryFlag {
55 if (CFAbsoluteTimeGetCurrent() < _outOfMemoryResetTime)
56 return;
57 _outOfMemoryResetTime = 0;
58 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag];
59 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.
60 }
61
62 - (void)resetForegroundMemoryWarningCount {
63 _foregroundMemoryWarningCount = 0;
64 }
65
66 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698