Skip to content

Commit a692ef9

Browse files
authored
library: Port Memory Monitor demo to Vala (#770)
* library: Port Memory Monitor demo to Vala * Link `MemoryMonitorWarningLevel` in Memory Monitor Vala demo
1 parent b544282 commit a692ef9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env -S vala workbench.vala --pkg libadwaita-1
2+
3+
private HashTable<string, int?> cache;
4+
5+
public void main () {
6+
cache = new HashTable<string, int?> (string.hash, str_equal);
7+
cache["a"] = 1;
8+
cache["b"] = 2;
9+
cache["c"] = 3;
10+
11+
var memory_monitor = MemoryMonitor.dup_default ();
12+
memory_monitor.low_memory_warning.connect ((level) => {
13+
/*
14+
* Use inequalities for checking as new levels may be added in the future
15+
* See https://valadoc.org/gio-2.0/GLib.MemoryMonitorWarningLevel.html
16+
*/
17+
if (level >= MemoryMonitorWarningLevel.LOW) {
18+
// Processes should free up unneeded resources
19+
message ("Warning Level: Low");
20+
drop_caches ();
21+
}
22+
23+
if (level >= MemoryMonitorWarningLevel.MEDIUM) {
24+
// Processes should try harder to free unneeded resources
25+
message ("Warning Level: Medium");
26+
}
27+
28+
if (level >= MemoryMonitorWarningLevel.CRITICAL) {
29+
// The system will start terminating processes to reclaim memory
30+
message ("Warning Level: Critical");
31+
}
32+
});
33+
}
34+
35+
private void drop_caches () {
36+
cache.remove_all ();
37+
}

0 commit comments

Comments
 (0)