Skip to content

Reduce idle memory: fonts on demand, trim after a tray start, malloc trim on Linux - #1132

Merged
KnifeLemon merged 1 commit into
mainfrom
fix/idle-memory-957
Nov 4, 2026
Merged

KnifeLemon merged 1 commit into
mainfrom
fix/idle-memory-957

Conversation

@KnifeLemon

@KnifeLemon KnifeLemon commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Refs #957

Filee's memory while waiting in the tray is almost all native, not managed. This PR removes what could safely go and makes the next Linux log show the split. It builds on #1121, which it needs: on-demand fonts rely on a letter being matched only in the family asked for.

Nothing here was measured on Linux (no WSL distribution or Docker on the machine). Every number below is Windows x64, Release, from two runs unless noted. The Linux changes are compiled (-r linux-x64) but have never been run.

What fills idle memory (Windows)

  • Managed heap: 3-7 MB in every idle run. So GC and JIT settings have little to work on.
  • Embedded fonts: about 45 MB private and 82 MB of working set. Avalonia opens all 18 files at start, and SKTypeface.FromStream copies each into native memory (read from its IL, so this also applies on Linux).
  • GPU stack: about 38-48 MB private right after a tray start, and about 100 MB after the main window has been open (private 192 MB with the GPU, 86 MB with FILEE_RENDERING=software). Avalonia's GLX setup also creates its context at start (read from its IL).

Changes

  • Fonts on demand (FileeFontCollection, combined with the wrapper from Draw Han in the UI language's own font #1121): each family is read when the font chain first asks for it. A letter is still matched only in the family asked for, so families later in the chain stay unread: in an English or Korean UI, "→" and Han read Noto Sans KR, and JP and SC stay out.
  • Trim after a tray start: nothing asked for a trim before unless a job finished or a window closed.
  • Aggressive collection in the trim: the old comment said forced collections decommit empty regions. Measured, they don't.
  • Linux: malloc_trim in the trim, looked up by name in libc.so.6, so musl skips it.
  • Linux log line: own (USS), resident, anonymous, managed heap and committed, malloc used and free. Process.PrivateMemorySize64 reads VmData there, which isn't what smem shows.
  • Linux FILEE_RENDERING=software (X11), opt-in. I left the default alone because I can't test the donut's animation on Linux.
  • Closing the crop, image edit and PDF page windows now requests a trim, as the other tool windows do.

On Windows and macOS the fonts, the tray-start trim, the aggressive collection and the trim on closing those windows also apply. The Windows trim log line gains the committed size.

Measured (Windows)

The baseline is main before #1121, which reads every font at the start. #1121 itself wasn't measured on its own; it doesn't change what is read.

Scenario main this PR
Tray start, 20 s, English, working set / private 210-214 / 111 MB 132-136 / 70-71 MB
Tray start, 60 s, English, working set / private 213 / 112 MB (one run) 20-25 / 64 MB
Window opened, then closed to the tray, English, private 186-188 MB 156-157 MB
Window opened, then closed to the tray, Korean, private 192 MB 159 MB

The working set after closing the window is about 21-26 MB in both builds, because the Windows trim already emptied it.

Freeing a 544 MB managed heap in a separate probe, then trimming:

Collection Committed after Private after Time
Forced (old) 422-434 MB 436-448 MB about 80 ms
Aggressive (new) 0 MB 9 MB 146-188 ms

In one of the two forced runs, committed had dropped to 88 MB three seconds later; in the other it stayed at 422 MB.

Tried at a tray start, no effect beyond noise (about 3 MB), so not changed: ReadyToRun, TieredPGO=0, TieredCompilation=0, gcConcurrent=0, GCConserveMemory=5, GCgen0size. ReadyToRun also made no difference after the window had been open, and it adds 44 MB to the publish folder.

Limits

  • Fonts stay once read: a family isn't released for the rest of the run. A language or text that walks further along the chain keeps more.
  • Not measured at all: the malloc_trim effect, and everything else in real Linux numbers. The new log line is there to get them from the reporter.

Left out

  • Limiting malloc's arenas: unmeasured on Linux, and it could slow in-process native work such as libvips. We're asking the reporter to compare with MALLOC_ARENA_MAX=2 first, and will decide from that number.
  • Lower SkiaOptions.MaxGpuResourceSizeBytes: 16 MB saved about 19 MB private, but a low cap risks re-uploading large previews every frame.
  • Creating the toast window on first use: about 9 MB private, one run; needs the toast's job subscription reworked.
  • AnnotationFonts: still keeps its own copies of the bold faces after the first label.

Tests

  • FileeFontCollectionTests and LocalizationResourceTests pass, including Draw Han in the UI language's own font #1121's expected fonts: zh-CN 将 in Noto Sans SC, ko in Noto Sans KR, ja in Noto Sans JP. A letter's font is also checked to be the same on a new collection as with every family already read.
  • MemoryTrimmerTests and NativeMemoryRenderTests pass on Windows.
  • LinuxMemoryTests: the parsing passes here. The glibc test is skipped on Windows, so CI on Linux will be its first run.
  • dotnet format --verify-no-changes is clean on the changed files.

🤖 Generated with Claude Code

…im malloc on Linux

Refs #957. Measured on Windows only (a Linux measurement wasn't possible here):
the managed heap is 3-7 MB at idle, so GC, JIT and ReadyToRun settings don't
matter. Native memory does.

- Embedded fonts are read family by family when text asks. SkiaSharp copies a font
  opened from a stream into native memory, so all 18 files (37 MB) stayed there
  from the start. FileeFontCollection keeps matching a letter only in the family
  asked for (#1095), so families later in the chain stay unread.
- A start without a window now requests a trim: nothing asked for one before.
- The trim uses an aggressive collection: a forced one keeps empty regions
  committed, contrary to what the comment said.
- Linux: malloc_trim after the collections, bound by name so musl is left alone.
- Linux: the trim logs own/resident/anonymous memory, the managed heap and malloc,
  because Process.PrivateMemorySize64 reports VmData there.
- Linux: FILEE_RENDERING=software, as the GPU driver stays loaded with no window.
- Closing the crop, image edit and PDF page windows also requests a trim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KnifeLemon
KnifeLemon merged commit 224f6a1 into main Nov 4, 2026
31 of 33 checks passed
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.

1 participant