diff --git a/src/Library/demos/Memory Monitor/main.blp b/src/Library/demos/Memory Monitor/main.blp new file mode 100644 index 000000000..3ba4ed8a0 --- /dev/null +++ b/src/Library/demos/Memory Monitor/main.blp @@ -0,0 +1,12 @@ +using Gtk 4.0; +using Adw 1; + +Adw.StatusPage { + title: _("Memory Monitor"); + description: _("Monitor system memory"); + + LinkButton { + label: "API Reference"; + uri: "https://docs.gtk.org/gio/iface.MemoryMonitor.html"; + } +} diff --git a/src/Library/demos/Memory Monitor/main.js b/src/Library/demos/Memory Monitor/main.js new file mode 100644 index 000000000..1f050c83b --- /dev/null +++ b/src/Library/demos/Memory Monitor/main.js @@ -0,0 +1,27 @@ +import Gio from "gi://Gio"; + +const memory_monitor = Gio.MemoryMonitor.dup_default(); + +const cache = new Map(); +cache.set("a", 1); +cache.set("b", 2); +cache.set("c", 3); + +memory_monitor.connect("low-memory-warning", (monitor, level) => { + // Use inequalities for checking as new levels may be added in the future + if (level >= Gio.MemoryMonitorWarningLevel.LOW) { + // Processes should free up unneeded resources + console.log("Warning Level: Low"); + drop_caches(); + } else if (level >= Gio.MemoryMonitorWarningLevel.MEDIUM) { + // Processes should try harder to free up unneeded resources + console.log("Warning Level: Medium"); + } else if (level >= Gio.MemoryMonitorWarningLevel.CRITICAL) { + // system will start terminating processes to reclaim memory + console.log("Warning Level: Critical"); + } +}); + +function drop_caches() { + cache.clear(); +} diff --git a/src/Library/demos/Memory Monitor/main.json b/src/Library/demos/Memory Monitor/main.json new file mode 100644 index 000000000..52c034017 --- /dev/null +++ b/src/Library/demos/Memory Monitor/main.json @@ -0,0 +1,6 @@ +{ + "category": "platform", + "description": "Monitor system memory", + "panels": ["code", "preview"], + "autorun": true +}