Skip to content

ImageAtlas retains empty <= 1024x1024 shelf pages when higher pages are active and never evicts dead Slots on shared pages #2103

Done

Description

@wmfeht

Tested on dev at 452fd21ae6faced35a6cb296bc0d7dc7b100450b.

Problem

ImageAtlas (crates/rune-image/src/pixels.rs) has two memory-retention gaps for images <= IMAGE_ATLAS_SIZE (1024×1024), such as thumbnails, icons, and 1× PDF/document preview pages:

  1. Empty <= 1024×1024 shelf page buffers are retained when a higher-indexed page is active:

    • In ImageAtlas::end_frame() (crates/rune-image/src/pixels.rs:643-663), when a page has zero remaining slots, page.data = Vec::new() is only executed if page.width > IMAGE_ATLAS_SIZE || page.height > IMAGE_ATLAS_SIZE (:656-658).
    • If an empty 1024×1024 shelf page sits at index i < keep (keep = self.slots.values().map(|s| s.page + 1).max().unwrap_or(0) at :661-662, e.g. because a later page still holds active thumbnails), page.data (up to 4 MiB CPU) is kept non-empty.
    • Because page.data.is_empty() stays false, Renderer::upload_images() (crates/rune/src/render/upload.rs:166) also keeps its 4 MiB GPU wgpu::Texture allocated until a full rest() occurs (10 s idle) and another frame renders.
  2. Dead Slot entries on shelf pages shared with persistently visible images are never evicted:

    • Slot (crates/rune-image/src/pixels.rs:453-462) has no per-slot touched / shown flag; liveness is tracked only per ImagePage (touched / shown, :475-477).
    • Both end_frame() (self.slots.retain(|_, s| pages[s.page].touched || (s.page == 0 && first_kept)), :646) and rest() (self.slots.retain(|_, s| pages[s.page].shown), :670) keep every slot on any page that drew even a single persistent icon/thumbnail.
    • When transient Pixels (e.g. preview pages or scrolled-past thumbnails <= 1024×1024) share a shelf page with one persistently visible image, their Slot entries (and any self.starts animation entries at :649, :672) are never removed, the page can never reset (:653, :674, :706), and subsequent allocations spill into additional pages.

Expected behavior

  • Free page.data on empty shelf pages (at least i > 0, or after a short idle frame grace) so both CPU and GPU memory for unused shelf pages are reclaimed promptly even when higher-indexed pages remain active.
  • Track per-slot liveness (or prune dead Slots whose Pixels id is no longer drawn / dropped) so shared shelf pages don't accumulate dead Slot entries or leak shelf space indefinitely when paired with a persistently visible icon/thumbnail.

Activity

  1. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I reproduced both problems on dev (0faedf34) with unit tests against ImageAtlas in crates/rune-image/src/pixels.rs.

    1. An empty shelf page below an active page keeps its buffer

    Setup: frame 1 draws four 512×512 images that stay on page 0, four 512×512 thumbnails that go to page 1, and one 64×64 image that goes to page 2. Then three frames draw only the 64×64 image, each followed by end_frame().

    page 0: 1024x1024 data=4194304 bytes slots=4
    page 1: 1024x1024 data=4194304 bytes slots=0   <- empty, 4 MiB kept
    page 2: 1024x128  data=524288 bytes  slots=1
    

    Page 1 has no slots but keeps its 4 MiB buffer. end_frame() only frees data on pages larger than IMAGE_ATLAS_SIZE, and truncate(keep) can't drop a page that sits below page 2. Because the buffer isn't empty, Renderer::upload_images keeps that page's texture as well. Only rest() frees the buffer.

    2. Dead slots on a page that shares space with a persistent image

    Setup: each of 40 frames draws one 64×64 image that stays on screen, plus four new 256×256 thumbnails that are never drawn again and are dropped right away. Each frame ends with end_frame(), and rest() runs at the end.

    after frame 9..39: pages=2 slots=20 cpu bytes=8388608
    page 0: live slots=1 dropped-Pixels slots=15 shown=true
    page 1: live slots=0 dropped-Pixels slots=4  shown=true
    after rest: pages=2 slots=20
    

    The 15 slots for dropped Pixels on page 0 survive every end_frame() and also rest(), because the persistent image keeps the whole page marked as drawn. Page 0 never starts over, so every new thumbnail goes to page 1, which costs another 4 MiB. Page 1's 4 slots are from the last frame and are expected. In this test the total stayed at 2 pages; I didn't test whether it keeps growing when more persistent images end up on other pages.

  2. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    Thanks again for the report. I've opened a PR that:

    • frees the buffer of an empty shelf page at the end of the frame, even when a later page is still in use. upload_images then drops its texture too. The first page keeps its buffer, as before.
    • removes an image's slot at the end of the frame once every Pixels handle for it is dropped, even on a page another image still draws from.
    • makes rest() track slots one by one, so it forgets images the last frame didn't draw even when they share a page with one it did.

    One part isn't covered: space freed on a page that's still in use is only reused once the whole page empties. Reusing it sooner would mean moving images that are on screen. The PR adds two tests based on the reproduction above.

    #2107

  3. Hona commented on Nov 25, 2026

    @Hona
    Member

    Both parts are fixed on dev:

    1. Empty middle shelf pages — fixed in fix(image): free empty shelf page buffers in ImageAtlas::end_frame when higher pages remain active #2115 (ce1caa8a): ImageAtlas::end_frame previously only cleared page.data when a page was larger than 1024x1024, so an empty shelf page below an active higher page kept its 4 MiB CPU buffer and its 4 MiB GPU texture in Renderer::upload_images. It now frees page.data on every page left with zero slots, dropping the 3-page reproduction from 8,912,896 B (page 1 = 4 MiB CPU + 4 MiB GPU) to 4,718,592 B (page 1 = 0 B CPU, GPU texture dropped).
    2. Dead slots on shared pages and atlas bounds — fixed in fix(image): reclaim dead slots on shared atlas pages and bound ImageAtlas in pages, slots, and bytes #2124 (037775cd): Slot now tracks per-slot liveness (Weak<FrameInner>, touched, shown, and last-drawn frame) and shelf pages reclaim freed horizontal spans and coalesced/trailing empty bands without moving live slots. end_frame drops dead undrawn slots on shared pages immediately; rest() forgets undrawn slots per slot (including on page 0) and shrinks remaining shelf pages to their occupied rows. On the 40-frame shared-page reproduction (1 persistent 64×64 image + 4 dropped 256×256 thumbnails/frame), memory dropped from 2 pages / 20 slots / 8,388,608 B (before and after rest()) to 1 page / 5 slots / 4,194,304 B after frame 39 and 1 page / 1 slot / 524,288 B after rest().

    ImageAtlas retention across frames is now bounded to 16 pages (IMAGE_ATLAS_MAX_PAGES), 64 MiB of decoded pixels (IMAGE_ATLAS_MAX_BYTES = 67,108,864 B), and 4,096 slots (IMAGE_ATLAS_MAX_SLOTS) with LRU eviction of unshown entries and next-frame relocation of sparse overflow pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions