Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Library/demos/Memory Monitor/main.blp
Original file line number Diff line number Diff line change
@@ -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";
}
}
27 changes: 27 additions & 0 deletions src/Library/demos/Memory Monitor/main.js
Original file line number Diff line number Diff line change
@@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is great overall! One comment: this should perhaps be a separate if rather than an else if, otherwise drop_caches() will only be called on levels between LOW and MEDIUM, rather than on all levels >= LOW.

Copy link
Contributor Author

@AkshayWarrier AkshayWarrier Aug 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually wait, now that I think about it, won't it behave in a completely oppositive manner? For all levels >= LOW, the first if condition would always be satisfied and it would never get to the other else if(s) even if level >= MEDIUM. I can't believe I missed this but still changing the else if to if should fix the issue regardless.

// 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();
}
6 changes: 6 additions & 0 deletions src/Library/demos/Memory Monitor/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "platform",
"description": "Monitor system memory",
"panels": ["code", "preview"],
"autorun": true
}