A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

0 downloads

dbbasic-browser/apps/browser/src/process-memory.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 site icons (favicons) in tabs, address bar suggestions and the bookmarks panel 7249579 15 hours, 50 minutes ago History
process-memory.ts · 2.6 KB · 56 lines
1/**
2 * What each process of the browser really costs the machine.
3 *
4 * "Resident" memory (what Electron reports as workingSetSize, and what `ps` shows) is not that number on macOS: the system
5 * squeezes memory a page is not using into compressed memory and counts only what is left in RAM. A tab holding 400 MB can show
6 * as 80 MB resident while still taking 400 MB from the machine. Activity Monitor's "Memory" column is the physical footprint,
7 * which includes the compressed part, and the `footprint` tool that ships with macOS reports it. So on macOS we ask it (about
8 * a tenth of a second for the whole browser); anywhere else, or if it fails, we use what Electron gives us.
9 */
10
11import { execFile } from "node:child_process";
12import { mkdtemp, readFile, rm } from "node:fs/promises";
13import { tmpdir } from "node:os";
14import { join } from "node:path";
15
16/** The per-process footprints in a `footprint --json` document, in bytes, by pid. */
17export function parseFootprint(text: string): Map<number, number> {
18 const out = new Map<number, number>();
19 let j: unknown;
20 try {
21 j = JSON.parse(text);
22 } catch {
23 return out;
24 }
25 const procs = (j as { processes?: unknown }).processes;
26 if (!Array.isArray(procs)) return out;
27 for (const p of procs as Array<Record<string, unknown>>) {
28 const aux = p.auxiliary as Record<string, unknown> | undefined;
29 const bytes = typeof aux?.phys_footprint === "number" ? aux.phys_footprint : typeof p.footprint === "number" ? p.footprint : null;
30 if (typeof p.pid === "number" && bytes !== null && bytes >= 0) out.set(p.pid, bytes);
31 }
32 return out;
33}
34
35export type Run = (file: string, args: string[]) => Promise<void>;
36const run: Run = (file, args) =>
37 new Promise((resolve, reject) => {
38 execFile(file, args, { timeout: 5000, windowsHide: true }, (e) => (e ? reject(e) : resolve()));
39 });
40
41/** Footprint in bytes for the given pids, on macOS; an empty map anywhere else or when the tool fails. */
42export async function macFootprints(pids: number[], platform: string = process.platform, exec: Run = run): Promise<Map<number, number>> {
43 if (platform !== "darwin" || pids.length === 0) return new Map();
44 const safe = [...new Set(pids.filter((p) => Number.isInteger(p) && p > 0))];
45 let dir: string | null = null;
46 try {
47 dir = await mkdtemp(join(tmpdir(), "dbb-fp-"));
48 const file = join(dir, "fp.json");
49 await exec("/usr/bin/footprint", [...safe.flatMap((p) => ["-p", String(p)]), "--noCategories", "-j", file]);
50 return parseFootprint(await readFile(file, "utf8"));
51 } catch {
52 return new Map();
53 } finally {
54 if (dir) await rm(dir, { recursive: true, force: true }).catch(() => {});
55 }
56}