Copyright | Will Thompson Iñaki García Etxebarria and Jonas Platte |
---|---|
License | LGPL-2.1 |
Maintainer | Iñaki García Etxebarria |
Safe Haskell | None |
Language | Haskell2010 |
MemoryMonitor
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
See 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 lowMemoryWarning
signal, and unref the MemoryMonitor
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 C_MemoryMonitorLowMemoryWarningCallback = Ptr () -> CUInt -> Ptr () -> IO ()
- type MemoryMonitorLowMemoryWarningCallback = MemoryMonitorWarningLevel -> IO ()
- afterMemoryMonitorLowMemoryWarning :: (IsMemoryMonitor a, MonadIO m) => a -> MemoryMonitorLowMemoryWarningCallback -> m SignalHandlerId
- genClosure_MemoryMonitorLowMemoryWarning :: MonadIO m => MemoryMonitorLowMemoryWarningCallback -> m (GClosure C_MemoryMonitorLowMemoryWarningCallback)
- mk_MemoryMonitorLowMemoryWarningCallback :: C_MemoryMonitorLowMemoryWarningCallback -> IO (FunPtr C_MemoryMonitorLowMemoryWarningCallback)
- noMemoryMonitorLowMemoryWarningCallback :: Maybe MemoryMonitorLowMemoryWarningCallback
- onMemoryMonitorLowMemoryWarning :: (IsMemoryMonitor a, MonadIO m) => a -> MemoryMonitorLowMemoryWarningCallback -> m SignalHandlerId
- wrap_MemoryMonitorLowMemoryWarningCallback :: MemoryMonitorLowMemoryWarningCallback -> C_MemoryMonitorLowMemoryWarningCallback
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 # | |
IsGValue MemoryMonitor Source # | Convert |
Defined in GI.Gio.Interfaces.MemoryMonitor toGValue :: MemoryMonitor -> IO GValue # fromGValue :: GValue -> IO MemoryMonitor # | |
ManagedPtrNewtype MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor | |
TypedObject MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor | |
GObject MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.MemoryMonitor | |
HasParentTypes MemoryMonitor Source # | |
Defined in GI.Gio.Interfaces.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
Overloaded methods
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 C_MemoryMonitorLowMemoryWarningCallback = Ptr () -> CUInt -> Ptr () -> IO () Source #
Type for the callback on the (unwrapped) C side.
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 -> 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
genClosure_MemoryMonitorLowMemoryWarning :: MonadIO m => MemoryMonitorLowMemoryWarningCallback -> m (GClosure C_MemoryMonitorLowMemoryWarningCallback) Source #
Wrap the callback into a GClosure
.
mk_MemoryMonitorLowMemoryWarningCallback :: C_MemoryMonitorLowMemoryWarningCallback -> IO (FunPtr C_MemoryMonitorLowMemoryWarningCallback) Source #
Generate a function pointer callable from C code, from a C_MemoryMonitorLowMemoryWarningCallback
.
noMemoryMonitorLowMemoryWarningCallback :: Maybe MemoryMonitorLowMemoryWarningCallback Source #
A convenience synonym for
.Nothing
:: Maybe
MemoryMonitorLowMemoryWarningCallback
onMemoryMonitorLowMemoryWarning :: (IsMemoryMonitor a, MonadIO m) => 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