1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsThreadUtils.h"
10 #include "nsIObserver.h"
11 #include "nsIObserverService.h"
12 #include "nsIRunnable.h"
13 #include "nsISimpleEnumerator.h"
16 #include "mozilla/Services.h"
17 #include "mozilla/Atomics.h"
22 // Minimum memory threshold for a device to be considered
23 // a low memory platform. This value has be in sync with
24 // Java's equivalent threshold, defined in HardwareUtils.java
25 # define LOW_MEMORY_THRESHOLD_KB (384 * 1024)
28 static mozilla::Atomic
<bool> sIsFlushing
;
29 static PRIntervalTime sLastFlushTime
= 0;
32 bool nsMemory::IsLowMemoryPlatform() {
34 static int sLowMemory
=
35 -1; // initialize to unknown, lazily evaluate to 0 or 1
36 if (sLowMemory
== -1) {
37 sLowMemory
= 0; // assume "not low memory" in case file operations fail
39 // check if MemTotal from /proc/meminfo is less than LOW_MEMORY_THRESHOLD_KB
40 FILE* fd
= fopen("/proc/meminfo", "r");
45 int rv
= fscanf(fd
, "MemTotal: %" PRIu64
" kB", &mem
);
52 sLowMemory
= (mem
< LOW_MEMORY_THRESHOLD_KB
) ? 1 : 0;
54 return (sLowMemory
== 1);
60 static void RunFlushers(const char16_t
* aReason
) {
61 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
64 // os->NotifyObservers(this, "memory-pressure", aReason);
65 // we are going to do this manually to see who/what is
68 nsCOMPtr
<nsISimpleEnumerator
> e
;
69 os
->EnumerateObservers("memory-pressure", getter_AddRefs(e
));
72 nsCOMPtr
<nsIObserver
> observer
;
75 while (NS_SUCCEEDED(e
->HasMoreElements(&loop
)) && loop
) {
76 nsCOMPtr
<nsISupports
> supports
;
77 e
->GetNext(getter_AddRefs(supports
));
83 observer
= do_QueryInterface(supports
);
84 observer
->Observe(observer
, "memory-pressure", aReason
);
92 static nsresult
FlushMemory(const char16_t
* aReason
, bool aImmediate
) {
94 // They've asked us to run the flusher *immediately*. We've
95 // got to be on the UI main thread for us to be able to do
97 if (!NS_IsMainThread()) {
98 NS_ERROR("can't synchronously flush memory: not on UI thread");
99 return NS_ERROR_FAILURE
;
103 bool lastVal
= sIsFlushing
.exchange(true);
108 PRIntervalTime now
= PR_IntervalNow();
110 // Run the flushers immediately if we can; otherwise, proxy to the
111 // UI thread and run 'em asynchronously.
114 RunFlushers(aReason
);
116 // Don't broadcast more than once every 1000ms to avoid being noisy
117 if (PR_IntervalToMicroseconds(now
- sLastFlushTime
) > 1000) {
118 nsCOMPtr
<nsIRunnable
> runnable(NS_NewRunnableFunction(
120 [reason
= aReason
]() -> void { RunFlushers(reason
); }));
121 NS_DispatchToMainThread(runnable
.forget());
125 sLastFlushTime
= now
;
129 nsresult
nsMemory::HeapMinimize(bool aImmediate
) {
130 return FlushMemory(u
"heap-minimize", aImmediate
);