1 // Copyright 2015 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.
5 #include "components/memory_pressure/direct_memory_pressure_calculator.h"
7 namespace memory_pressure
{
11 static const int kKBperMB
= 1024;
15 // A system is considered 'high memory' if it has more than 1.5GB of system
16 // memory available for use by the memory manager (not reserved for hardware
17 // and drivers). This is a fuzzy version of the ~2GB discussed below.
18 const int DirectMemoryPressureCalculator::kLargeMemoryThresholdMb
= 1536;
20 // These are the default thresholds used for systems with < ~2GB of physical
21 // memory. Such systems have been observed to always maintain ~100MB of
22 // available memory, paging until that is the case. To try to avoid paging a
23 // threshold slightly above this is chosen. The moderate threshold is slightly
24 // less grounded in reality and chosen as 2.5x critical.
26 DirectMemoryPressureCalculator::kSmallMemoryDefaultModerateThresholdMb
=
29 DirectMemoryPressureCalculator::kSmallMemoryDefaultCriticalThresholdMb
=
32 // These are the default thresholds used for systems with >= ~2GB of physical
33 // memory. Such systems have been observed to always maintain ~300MB of
34 // available memory, paging until that is the case.
36 DirectMemoryPressureCalculator::kLargeMemoryDefaultModerateThresholdMb
=
39 DirectMemoryPressureCalculator::kLargeMemoryDefaultCriticalThresholdMb
=
42 DirectMemoryPressureCalculator::DirectMemoryPressureCalculator()
43 : moderate_threshold_mb_(0), critical_threshold_mb_(0) {
47 DirectMemoryPressureCalculator::DirectMemoryPressureCalculator(
48 int moderate_threshold_mb
,
49 int critical_threshold_mb
)
50 : moderate_threshold_mb_(moderate_threshold_mb
),
51 critical_threshold_mb_(critical_threshold_mb
) {
52 DCHECK_GE(moderate_threshold_mb_
, critical_threshold_mb_
);
53 DCHECK_LE(0, critical_threshold_mb_
);
56 DirectMemoryPressureCalculator::MemoryPressureLevel
57 DirectMemoryPressureCalculator::CalculateCurrentPressureLevel() {
58 base::SystemMemoryInfoKB mem_info
= {};
59 if (!GetSystemMemoryInfo(&mem_info
))
60 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
;
62 // How much system memory is actively available for use right now, in MBs.
63 int phys_free
= mem_info
.free
/ kKBperMB
;
65 // TODO(chrisha): This should eventually care about address space pressure,
66 // but the browser process (where this is running) effectively never runs out
67 // of address space. Renderers occasionally do, but it does them no good to
68 // have the browser process monitor address space pressure. Long term,
69 // renderers should run their own address space pressure monitors and act
70 // accordingly, with the browser making cross-process decisions based on
71 // system memory pressure.
73 // Determine if the physical memory is under critical memory pressure.
74 if (phys_free
<= critical_threshold_mb_
)
75 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
;
77 // Determine if the physical memory is under moderate memory pressure.
78 if (phys_free
<= moderate_threshold_mb_
)
79 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
;
81 // No memory pressure was detected.
82 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE
;
85 void DirectMemoryPressureCalculator::InferThresholds() {
86 // Determine if the memory installed is 'large' or 'small'. Default to 'large'
87 // on failure, which uses more conservative thresholds.
88 bool large_memory
= true;
89 base::SystemMemoryInfoKB mem_info
= {};
90 if (GetSystemMemoryInfo(&mem_info
)) {
91 large_memory
= mem_info
.total
/ kKBperMB
>=
92 DirectMemoryPressureCalculator::kLargeMemoryThresholdMb
;
96 moderate_threshold_mb_
= kLargeMemoryDefaultModerateThresholdMb
;
97 critical_threshold_mb_
= kLargeMemoryDefaultCriticalThresholdMb
;
99 moderate_threshold_mb_
= kSmallMemoryDefaultModerateThresholdMb
;
100 critical_threshold_mb_
= kSmallMemoryDefaultCriticalThresholdMb
;
104 } // namespace memory_pressure