gi-gio-2.0.27: Gio bindings
CopyrightWill Thompson Iñaki García Etxebarria and Jonas Platte
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria
Safe HaskellNone
LanguageHaskell2010

GI.Gio.Interfaces.MemoryMonitor

Description

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

Exported types

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

Instances details
(GObject o, IsDescendantOf MemoryMonitor o) => IsMemoryMonitor o Source # 
Instance details

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 #

Arguments

:: (HasCallStack, MonadIO m) 
=> m MemoryMonitor

Returns: a new reference to the default MemoryMonitor

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 #

Arguments

 = MemoryMonitorWarningLevel

level: the MemoryMonitorWarningLevel warning level

-> 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

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