Skip to content

macOS: reduce idle memory footprint with windowless tray agents - #50

Closed
mikhail-angelov wants to merge 1 commit into
tarwin:mainfrom
mikhail-angelov:feat/macos-headless-tray
Closed

mikhail-angelov wants to merge 1 commit into
tarwin:mainfrom
mikhail-angelov:feat/macos-headless-tray

Conversation

@mikhail-angelov

Copy link
Copy Markdown
Contributor

macOS: reduce idle memory footprint with windowless tray agents

Tray apps currently construct a main WKWebView even with accessory activation.
A background filesystem watcher therefore pays for a hidden browser just to
expose its menu-bar icon.

Goals

  • Reduce an application's memory footprint while its background work does not
    need a WebView. Keep the backend, native tray and menus available without
    constructing a main window or starting WebKit worker processes.
  • Let the UI lifecycle be independent of the background worker: open a WebView
    when needed, close its window while the worker stays alive, and open it again.
    Applications needing complete editor-process teardown can launch their editor
    separately from the windowless agent.

The target is removing the always-on WebView cost. This PR does not establish
5 MiB as a memory budget or guarantee that closing an in-process window releases
all WebKit memory.

Implementation

Add opt-in headless: true to macOS configuration and createApp options.
Start the native AppKit host and backend without a main window or WebView;
retain native tray commands, named windows, their bridge and close callbacks
on the existing protocol. Frontend/index.html is optional at startup.

Both modes share one AppKit dispatcher and event loop. Only startup
initialization differs; no additional runtime, process or protocol is introduced.
Application visibility works without a main window; headless reload targets
open named pages. Dev skips watching an absent frontend directory, and
missing-window state is nullable in the types.

Dev passes TINYJS_HEADLESS; packaged apps use a TinyjsHeadless plist boolean.
Main-window APIs do not implicitly create a window. Headless named windows
use a native UIDelegate for file-picker and dialog callbacks normally registered
by the webview library during construction of its main view.

Scope and limitations

macOS implementation only. Windows/Linux explicitly reject the enabled flag.
Intel/macOS 14 and Windows/Linux native builds were not run. On-device AI was
disabled for the source build. This is a GUI-session agent, not an SSH/server mode.

WebKit may cache workers after an in-process window closes. Window destruction
and an empty window list therefore do not prove full memory teardown. A separate
editor process is an application-level deployment choice, not a process manager
introduced or automatically configured by this PR.

Evidence against the goals

A paired before/after run was performed on macOS 26.6.2 / Apple Silicon,
txiki.js 26.6.0. The baseline SDK was built from unmodified upstream commit
8336e31d5d03329051b13284730d98f9ec2682ca; the other SDK included this patch.
Both launchers were built using TINYJS_AI=0 ./setup.sh and the same toolchain.

Both runs used ad-hoc signed packaged apps with the same backend, native tray,
empty index.html, editor.html, window size and accessory activation. The
baseline had no headless flag; the patched app enabled headless: true.
The baseline therefore kept its hidden main WebView, while the patched app
started without one. Both opened and closed the same named editor twice.
The runs were sequential, with the first app exiting before the second began.

Memory is the sum of Physical footprint from vmmap -summary for the launcher,
its backend and newly started WebKit workers observed during that isolated run.
WebKit workers were identified from process snapshots before launch and at each
sample, since their parent PID can be 1. Raw process IDs and command lines were
retained. This method can be confounded by another app starting WebKit at the
same time; it is a local process-snapshot measurement, not system-wide RAM savings.
Values include compressed memory and are not disk size or virtual address space.
Individual vmmap figures are rounded, so totals below are approximate.

The editor opens at 7 s, closes at 15 s, reopens at 24 s and closes again at
32 s. Samples are taken at approximately 4, 11, 19, 28 and 36 s after launch.

State Before patch, MiB After patch, MiB Reduction, MiB
Tray only, before the first editor open 65.29 17.17 48.12
Editor open (first cycle) 85.11 58.35 26.76
Editor closed (first cycle) 70.77 45.15 25.62
Editor reopened (second cycle) 85.77 62.40 23.37
Editor closed again (second cycle) 71.35 62.06 9.29

Goal 1, lower background footprint: demonstrated. Before the first editor
open, memory fell from 65.29 to 17.17 MiB, approximately 74%.
The baseline had five processes (launcher, backend, GPU, Networking, WebContent);
the patched app had only two (launcher and backend) while its native tray worked.

Goal 2, independent window lifecycle: demonstrated. Both packaged runs
reported a live native tray and successful page bridge/store round trips on both
editor opens. The unpatched run reported BOOT ["main"], CLOSED ["main"] and
CLOSED_AGAIN ["main"]; the patched run reported BOOT [], CLOSED [] and
CLOSED_AGAIN []. Both closes emitted WINDOW_CLOSED editor, and the background
backend and tray remained alive. The patched dev path passed the same lifecycle
checks separately.

Full memory teardown after an in-process close is not demonstrated. In the
patched first cycle, WebContent exited but GPU/Networking remained; after the
second close, WebContent was still present at the sample. Neither close restored
the cold-start footprint. This is why the goal is avoiding an always-on main
WebView, with separate editor processes available for strict teardown, rather
than promising that window closure releases all WebKit memory.

Lifecycle reproduction is documented in test/headless-tray/README.md.
For the paired run, identical temporary copies of that fixture included an
empty index.html in both versions to satisfy the baseline's main-page requirement.
The normal headless fixture still deliberately has no index.html.
Raw measurements, per-process vmmap output, build logs, lifecycle logs and the
measurement script are retained locally under .build/memory-comparison/;
these local artifacts are not shipped as part of the PR.

Additional checks passed:

  • TINYJS_AI=0 ./setup.sh: native macOS ARM64 launcher builds.
  • node --check cli.js, node --check runtime/bridge.js, git diff --check.
  • codesign --verify --deep --strict for the packaged fixture.
  • A backend-only app with no frontend directory builds and starts, including dev.
  • A regular app still starts with ["main"] and loads its page.
  • Invalid flag types fail. Simulated Windows/Linux runtime selection rejects
    headless mode; this is not a native platform build test.
  • Native confirm/prompt callbacks return true / value on both editor loads
    using the existing native default-button test hook. File-picker and physical
    tray clicking remain manual checks in the fixture README.

@tarwin

tarwin commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thanks @mikhail-angelov this makes a lot of sense. I am going to get this working using your PR as long as I can make it work cross platform as well. Looking at it now.

@tarwin tarwin closed this in 8cb0780 Oct 12, 2026
tarwin added a commit that referenced this pull request Oct 12, 2026
No main window or WebView until the app opens a named one, on macOS,
Windows and Linux (#50, macOS by Mikhail Angelov). Linux named windows
open with the current app.icon(). Discord link in the README and site.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@tarwin

tarwin commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thanks @mikhail-angelov a new release has now been deployed with your changes - and more that help it work in a similar way cross platform.

Main thing to know is I changed the setting to mainWindow: false

@tarwin

tarwin commented Oct 12, 2026

Copy link
Copy Markdown
Owner

@mikhail-angelov sorry I screwed up the commit message so it doesn't show as merged - my mistake. Please know this did merge your changes. I'll try better next time - still new to this whole running an open source project thing.

@mikhail-angelov

Copy link
Copy Markdown
Contributor Author

thank you

feiyunwill pushed a commit to feiyunwill/tinyjsapp that referenced this pull request Oct 15, 2026
feiyunwill pushed a commit to feiyunwill/tinyjsapp that referenced this pull request Oct 15, 2026
…in#50)

- Rename the flag from "headless" to "mainWindow": false (tinyjs.json and
  createApp). "headless" already means "no display" across the launcher and
  skill docs; internals follow (g_windowless, TINYJS_NO_MAIN_WINDOW,
  TinyjsNoMainWindow plist key, test/windowless-tray).
- Windows/Linux no longer fail: they keep a hidden main window (accessory
  activation, blank page when there's no index.html) and the CLI prints a
  notice, so one tinyjs.json runs everywhere.
- Reject "url" combined with "mainWindow": false.
- Document why TinyWindowlessDelegate exists (install_open_handlers needs a
  delegate to attach openURLs/openFiles to).
- README, skill docs, types and TODO-verify updated; macOS (arm64 and
  x86_64 under Rosetta) verified.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants