Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gmemorymonitor.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright 2019 Red Hat, Inc
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "config.h"
22
#include "glib.h"
23
#include "glibintl.h"
24
25
#include "gmemorymonitor.h"
26
#include "ginetaddress.h"
27
#include "ginetsocketaddress.h"
28
#include "ginitable.h"
29
#include "gioenumtypes.h"
30
#include "giomodule-priv.h"
31
#include "gtask.h"
32
33
/**
34
 * SECTION:gmemorymonitor
35
 * @title: GMemoryMonitor
36
 * @short_description: Memory usage monitor
37
 * @include: gio/gio.h
38
 *
39
 * #GMemoryMonitor will monitor system memory and suggest to the application
40
 * when to free memory so as to leave more room for other applications.
41
 * It is implemented on Linux using the [Low Memory Monitor](https://gitlab.freedesktop.org/hadess/low-memory-monitor/)
42
 * ([API documentation](https://hadess.pages.freedesktop.org/low-memory-monitor/)).
43
 *
44
 * There is also an implementation for use inside Flatpak sandboxes.
45
 *
46
 * Possible actions to take when the signal is received are:
47
 *
48
 *  - Free caches
49
 *  - Save files that haven't been looked at in a while to disk, ready to be reopened when needed
50
 *  - Run a garbage collection cycle
51
 *  - Try and compress fragmented allocations
52
 *  - Exit on idle if the process has no reason to stay around
53
 *  - Call [`malloc_trim(3)`](man:malloc_trim) to return cached heap pages to
54
 *    the kernel (if supported by your libc)
55
 *
56
 * Note that some actions may not always improve system performance, and so
57
 * should be profiled for your application. `malloc_trim()`, for example, may
58
 * make future heap allocations slower (due to releasing cached heap pages back
59
 * to the kernel).
60
 *
61
 * See #GMemoryMonitorWarningLevel for details on the various warning levels.
62
 *
63
 * |[<!-- language="C" -->
64
 * static void
65
 * warning_cb (GMemoryMonitor *m, GMemoryMonitorWarningLevel level)
66
 * {
67
 *   g_debug ("Warning level: %d", level);
68
 *   if (warning_level > G_MEMORY_MONITOR_WARNING_LEVEL_LOW)
69
 *     drop_caches ();
70
 * }
71
 *
72
 * static GMemoryMonitor *
73
 * monitor_low_memory (void)
74
 * {
75
 *   GMemoryMonitor *m;
76
 *   m = g_memory_monitor_dup_default ();
77
 *   g_signal_connect (G_OBJECT (m), "low-memory-warning",
78
 *                     G_CALLBACK (warning_cb), NULL);
79
 *   return m;
80
 * }
81
 * ]|
82
 *
83
 * Don't forget to disconnect the #GMemoryMonitor::low-memory-warning
84
 * signal, and unref the #GMemoryMonitor itself when exiting.
85
 *
86
 * Since: 2.64
87
 */
88
89
/**
90
 * GMemoryMonitor:
91
 *
92
 * #GMemoryMonitor monitors system memory and indicates when
93
 * the system is low on memory.
94
 *
95
 * Since: 2.64
96
 */
97
98
/**
99
 * GMemoryMonitorInterface:
100
 * @g_iface: The parent interface.
101
 * @low_memory_warning: the virtual function pointer for the
102
 *  #GMemoryMonitor::low-memory-warning signal.
103
 *
104
 * The virtual function table for #GMemoryMonitor.
105
 *
106
 * Since: 2.64
107
 */
108
109
G_DEFINE_INTERFACE_WITH_CODE (GMemoryMonitor, g_memory_monitor, G_TYPE_OBJECT,
110
                              g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_INITABLE))
111
112
enum {
113
  LOW_MEMORY_WARNING,
114
  LAST_SIGNAL
115
};
116
117
static guint signals[LAST_SIGNAL] = { 0 };
118
119
/**
120
 * g_memory_monitor_dup_default:
121
 *
122
 * Gets a reference to the default #GMemoryMonitor for the system.
123
 *
124
 * Returns: (not nullable) (transfer full): a new reference to the default #GMemoryMonitor
125
 *
126
 * Since: 2.64
127
 */
128
GMemoryMonitor *
129
g_memory_monitor_dup_default (void)
130
0
{
131
0
  return g_object_ref (_g_io_module_get_default (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
132
0
                                                 "GIO_USE_MEMORY_MONITOR",
133
0
                                                 NULL));
134
0
}
135
136
static void
137
g_memory_monitor_default_init (GMemoryMonitorInterface *iface)
138
0
{
139
  /**
140
   * GMemoryMonitor::low-memory-warning:
141
   * @monitor: a #GMemoryMonitor
142
   * @level: the #GMemoryMonitorWarningLevel warning level
143
   *
144
   * Emitted when the system is running low on free memory. The signal
145
   * handler should then take the appropriate action depending on the
146
   * warning level. See the #GMemoryMonitorWarningLevel documentation for
147
   * details.
148
   *
149
   * Since: 2.64
150
   */
151
0
  signals[LOW_MEMORY_WARNING] =
152
0
    g_signal_new (I_("low-memory-warning"),
153
0
                  G_TYPE_MEMORY_MONITOR,
154
0
                  G_SIGNAL_RUN_LAST,
155
0
                  G_STRUCT_OFFSET (GMemoryMonitorInterface, low_memory_warning),
156
0
                  NULL, NULL,
157
0
                  NULL,
158
0
                  G_TYPE_NONE, 1,
159
0
                  G_TYPE_MEMORY_MONITOR_WARNING_LEVEL);
160
0
}