From 2d4553dcd3066580d84db96c0672fcda0b71889d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= Date: Wed, 1 Nov 2023 12:45:08 -0600 Subject: [PATCH 1/2] library: Port Memory Monitor demo to Vala --- src/Library/demos/Memory Monitor/main.vala | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/Library/demos/Memory Monitor/main.vala diff --git a/src/Library/demos/Memory Monitor/main.vala b/src/Library/demos/Memory Monitor/main.vala new file mode 100644 index 000000000..c236147eb --- /dev/null +++ b/src/Library/demos/Memory Monitor/main.vala @@ -0,0 +1,34 @@ +#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1 + +private HashTable cache; + +public void main () { + cache = new HashTable (string.hash, str_equal); + cache["a"] = 1; + cache["b"] = 2; + cache["c"] = 3; + + var memory_monitor = MemoryMonitor.dup_default (); + memory_monitor.low_memory_warning.connect ((level) => { + // Use inequalities for checking as new levels may be added in the future + if (level >= MemoryMonitorWarningLevel.LOW) { + // Processes should free up unneeded resources + message ("Warning Level: Low"); + drop_caches (); + } + + if (level >= MemoryMonitorWarningLevel.MEDIUM) { + // Processes should try harder to free unneeded resources + message ("Warning Level: Medium"); + } + + if (level >= MemoryMonitorWarningLevel.CRITICAL) { + // The system will start terminating processes to reclaim memory + message ("Warning Level: Critical"); + } + }); +} + +private void drop_caches () { + cache.remove_all (); +} From 18006bb140d9a67f092618d0f12ff3923344722d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= Date: Thu, 2 Nov 2023 22:01:54 -0600 Subject: [PATCH 2/2] Link `MemoryMonitorWarningLevel` in Memory Monitor Vala demo --- src/Library/demos/Memory Monitor/main.vala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Library/demos/Memory Monitor/main.vala b/src/Library/demos/Memory Monitor/main.vala index c236147eb..c744cac45 100644 --- a/src/Library/demos/Memory Monitor/main.vala +++ b/src/Library/demos/Memory Monitor/main.vala @@ -10,7 +10,10 @@ public void main () { var memory_monitor = MemoryMonitor.dup_default (); memory_monitor.low_memory_warning.connect ((level) => { - // Use inequalities for checking as new levels may be added in the future + /* + * Use inequalities for checking as new levels may be added in the future + * See https://valadoc.org/gio-2.0/GLib.MemoryMonitorWarningLevel.html + */ if (level >= MemoryMonitorWarningLevel.LOW) { // Processes should free up unneeded resources message ("Warning Level: Low");