gi-gio-2.0.32: Gio bindings
CopyrightWill Thompson and Iñaki García Etxebarria
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria
Safe HaskellSafe-Inferred
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
  • 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 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 MemoryMonitor itself when exiting.

Since: 2.64

Synopsis

Exported types

newtype MemoryMonitor Source #

Memory-managed wrapper type.

Constructors

MemoryMonitor (ManagedPtr MemoryMonitor) 

Instances

Instances details
Eq MemoryMonitor Source # 
Instance details

Defined in GI.Gio.Interfaces.MemoryMonitor

GObject MemoryMonitor Source # 
Instance details

Defined in GI.Gio.Interfaces.MemoryMonitor

ManagedPtrNewtype MemoryMonitor Source # 
Instance details

Defined in GI.Gio.Interfaces.MemoryMonitor

Methods

toManagedPtr :: MemoryMonitor -> ManagedPtr MemoryMonitor

TypedObject MemoryMonitor Source # 
Instance details

Defined in GI.Gio.Interfaces.MemoryMonitor

Methods

glibType :: IO GType

HasParentTypes MemoryMonitor Source # 
Instance details

Defined in GI.Gio.Interfaces.MemoryMonitor

IsGValue (Maybe MemoryMonitor) Source #

Convert MemoryMonitor to and from GValue. See toGValue and fromGValue.

Instance details

Defined in GI.Gio.Interfaces.MemoryMonitor

Methods

gvalueGType_ :: IO GType

gvalueSet_ :: Ptr GValue -> Maybe MemoryMonitor -> IO ()

gvalueGet_ :: Ptr GValue -> IO (Maybe MemoryMonitor)

type ParentTypes MemoryMonitor Source # 
Instance details

Defined in GI.Gio.Interfaces.MemoryMonitor

type ParentTypes MemoryMonitor = '[Initable, Object]

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

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