Copyright | Will Thompson and Iñaki García Etxebarria |
---|---|
License | LGPL-2.1 |
Maintainer | Iñaki García Etxebarria |
Safe Haskell | None |
Language | Haskell2010 |
GMemoryMonitor
will monitor system memory and suggest to the application
when to free memory so as to leave more room for other applications.
It is implemented on Linux using the
Low Memory Monitor
(API documentation).
There is also an implementation for use inside Flatpak sandboxes.
Possible actions to take when the signal is received are:
- Free caches
- Save files that haven’t been looked at in a while to disk, ready to be reopened when needed
- Run a garbage collection cycle
- Try and compress fragmented allocations
- Exit on idle if the process has no reason to stay around
- Call `malloc_trim(3)`) to return cached heap pages to the kernel (if supported by your libc)
Note that some actions may not always improve system performance, and so
should be profiled for your application. malloc_trim()
, for example, may
make future heap allocations slower (due to releasing cached heap pages back
to the kernel).
See [typegio
.MemoryMonitorWarningLevel] for details on the various warning
levels.
c code
static void warning_cb (GMemoryMonitor *m, GMemoryMonitorWarningLevel level) { g_debug ("Warning level: %d", level); if (warning_level > G_MEMORY_MONITOR_WARNING_LEVEL_LOW) drop_caches (); } static GMemoryMonitor * monitor_low_memory (void) { GMemoryMonitor *m; m = g_memory_monitor_dup_default (); g_signal_connect (G_OBJECT (m), "low-memory-warning", G_CALLBACK (warning_cb), NULL); return m; }
Don’t forget to disconnect the MemoryMonitor::lowMemoryWarning
signal, and unref the GMemoryMonitor
itself when exiting.
Since: 2.64
Synopsis
- newtype MemoryMonitor = MemoryMonitor (ManagedPtr MemoryMonitor)
- class (GObject o, IsDescendantOf MemoryMonitor o) => IsMemoryMonitor o
- toMemoryMonitor :: (MonadIO m, IsMemoryMonitor o) => o -> m MemoryMonitor
- memoryMonitorDupDefault :: (HasCallStack, MonadIO m) => m MemoryMonitor
- type MemoryMonitorLowMemoryWarningCallback = MemoryMonitorWarningLevel -> IO ()
- afterMemoryMonitorLowMemoryWarning :: (IsMemoryMonitor a, MonadIO m) => a -> ((?self :: a) => MemoryMonitorLowMemoryWarningCallback) -> m SignalHandlerId
- onMemoryMonitorLowMemoryWarning :: (IsMemoryMonitor a, MonadIO m) => a -> ((?self :: a) => MemoryMonitorLowMemoryWarningCallback) -> m SignalHandlerId
Exported types
newtype MemoryMonitor Source #
Memory-managed wrapper type.
Instances
Eq MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor (==) :: MemoryMonitor -> MemoryMonitor -> Bool # (/=) :: MemoryMonitor -> MemoryMonitor -> Bool # | |
GObject MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor | |
ManagedPtrNewtype MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor | |
TypedObject MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor | |
HasParentTypes MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor | |
IsGValue (Maybe MemoryMonitor) Source # | Convert |
Defined in GI.Gio.Interfaces.MemoryMonitor gvalueGType_ :: IO GType # gvalueSet_ :: Ptr GValue -> Maybe MemoryMonitor -> IO () # gvalueGet_ :: Ptr GValue -> IO (Maybe MemoryMonitor) # | |
type ParentTypes MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor |
class (GObject o, IsDescendantOf MemoryMonitor o) => IsMemoryMonitor o Source #
Type class for types which can be safely cast to MemoryMonitor
, for instance with toMemoryMonitor
.
Instances
(GObject o, IsDescendantOf MemoryMonitor o) => IsMemoryMonitor o Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor |
toMemoryMonitor :: (MonadIO m, IsMemoryMonitor o) => o -> m MemoryMonitor Source #
Cast to MemoryMonitor
, for types for which this is known to be safe. For general casts, use castTo
.
Methods
Click to display all available methods, including inherited ones
Methods
bindProperty, bindPropertyFull, forceFloating, freezeNotify, getv, init, isFloating, notify, notifyByPspec, ref, refSink, runDispose, stealData, stealQdata, thawNotify, unref, watchClosure.
Getters
getData, getProperty, getQdata.
Setters
dupDefault
memoryMonitorDupDefault Source #
:: (HasCallStack, MonadIO m) | |
=> m MemoryMonitor | Returns: a new reference to the default |
Gets a reference to the default MemoryMonitor
for the system.
Since: 2.64
Signals
lowMemoryWarning
type MemoryMonitorLowMemoryWarningCallback Source #
= MemoryMonitorWarningLevel |
|
-> IO () |
Emitted when the system is running low on free memory. The signal
handler should then take the appropriate action depending on the
warning level. See the MemoryMonitorWarningLevel
documentation for
details.
Since: 2.64
afterMemoryMonitorLowMemoryWarning :: (IsMemoryMonitor a, MonadIO m) => a -> ((?self :: a) => MemoryMonitorLowMemoryWarningCallback) -> m SignalHandlerId Source #
Connect a signal handler for the lowMemoryWarning signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
memoryMonitor #lowMemoryWarning callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onMemoryMonitorLowMemoryWarning :: (IsMemoryMonitor a, MonadIO m) => a -> ((?self :: a) => MemoryMonitorLowMemoryWarningCallback) -> m SignalHandlerId Source #
Connect a signal handler for the lowMemoryWarning signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
memoryMonitor #lowMemoryWarning callback