Skip to content

Buffering causes the application to crash when handling large files #31

Closed

Description

@RomanLiberda

What happened?

For large files (50–80 GB) on Apple TV 4K, buffering causes memory exhaustion.

After playback starts, memory usage can easily spike to around 1.4 GB. In most cases, the application crashes before the system has a chance to reclaim memory. I tried modifying the engine locally to buffer fewer segments, which helped somewhat. However, the problem reappears when subtitles are enabled — at that point, the application is usually terminated within a short time.

If I understand the architecture correctly:

  1. The main demuxer downloads the stream and converts it into video and audio HLS segments.
  2. The side demuxer downloads the same stream again, solely to extract subtitle packets.

This appears to result in an unavoidable application crash when dealing with files of this size.

Steps to reproduce

Apple TV 4K 2nd generation

  1. Try to open big stream (80GB)
    2.Try to activate subtitles

AetherEngine version or commit SHA

7e6ce64

Host app

Custom / my own integration

Platform

tvOS

OS version

tvOS 26.5

Device / chip

Apple TV 4K 2nd gen

Playback path

Native AVPlayer

Source media (for playback bugs)

MKV, HEVC, UHD, HDR

Error codes / log lines


Anything else

No response

Activity

  1. added
    bug
    on Jun 10, 2026
  2. superuser404notfound commented on Jun 10, 2026

    @superuser404notfound
    Owner

    Thanks for the detailed report. There were two separate problems here.

    The subtitle part is fixed as of f2e3260. Your read of the architecture was correct: the side demuxer that extracts subtitle packets opened a second connection and read to EOF at line rate, so it re-downloaded the whole file in parallel with playback and kept every future bitmap cue (each one holding a decoded RGBA image) in memory. The reader is now paced against the playhead: it reads at most 90 seconds ahead, then parks, and TCP backpressure throttles the second connection down to playback rate. For a PGS track this bounds cue memory to a few minutes around the playhead regardless of file size.

    The 1.4 GB spike without subtitles is a different story. The segment pipeline itself is disk-backed on your commit (segments are written to disk and served via sendfile, not held on the heap), so I'd like to narrow down what's growing. Could you share:

    1. Is the file Dolby Vision (profile 8.1), or plain HDR10? There is a suspected AVFoundation-internal leak in the per-frame HDR metadata path (~3 MB/s) that only affects DV sources.
    2. How are you measuring the 1.4 GB (Xcode memory gauge vs. a jetsam crash report)? If you have a jetsam report, the pageIns/phys_footprint numbers would help.
    3. Does the spike reproduce with the same file when you start playback and immediately pause?

    If you can update to current main and retest with subtitles enabled, that would confirm the first fix on your setup.

  3. RomanLiberda commented on Jun 10, 2026

    @RomanLiberda
    ContributorAuthor

    Thanks for the detailed report. There were two separate problems here.

    The subtitle part is fixed as of f2e3260. Your read of the architecture was correct: the side demuxer that extracts subtitle packets opened a second connection and read to EOF at line rate, so it re-downloaded the whole file in parallel with playback and kept every future bitmap cue (each one holding a decoded RGBA image) in memory. The reader is now paced against the playhead: it reads at most 90 seconds ahead, then parks, and TCP backpressure throttles the second connection down to playback rate. For a PGS track this bounds cue memory to a few minutes around the playhead regardless of file size.

    The 1.4 GB spike without subtitles is a different story. The segment pipeline itself is disk-backed on your commit (segments are written to disk and served via sendfile, not held on the heap), so I'd like to narrow down what's growing. Could you share:

    1. Is the file Dolby Vision (profile 8.1), or plain HDR10? There is a suspected AVFoundation-internal leak in the per-frame HDR metadata path (~3 MB/s) that only affects DV sources.
    2. How are you measuring the 1.4 GB (Xcode memory gauge vs. a jetsam crash report)? If you have a jetsam report, the pageIns/phys_footprint numbers would help.
    3. Does the spike reproduce with the same file when you start playback and immediately pause?

    If you can update to current main and retest with subtitles enabled, that would confirm the first fix on your setup.

    Size: 87.18 GB
    Bitrate: 80.0 Mb/s
    Video: HDR10
    Video Decoder: VideoToolbox HEVC (HW)
    Audio Decoder: Stream-copy (AC3)
    Mode: Native (AVPlayer)
    Duration: 2:25:21
    Tracks: 7 audio tracks • 3 subtitle tracks

    1. Just the Xcode

    2. No, when the video is paused it seems to be stable

    Here is the memprobe from the console:
    [AetherEngine] memprobe t=60s rss=466MB vmInt=427MB vmExt=33MB vmCmp=527MB vmIOS=0MB physFP=978MB mallocBlocks=739241 mallocMB=1865 avioFetchedMB=850 cacheCount=12 cacheMB=419 packetsWritten=1901 audioFifo=0 abFifoKB=0 abSwrKB=0 abTotKB=0 muxBytesMB=694 muxCuts=20 srvConns=1 srvBytesMB=520 srvSfMB=439 pktAlive=3 pktTotal=4698 subCues=20 audioTracks=7 subTracks=3 subActive=false avBufAhead=5.7s avBufBehind=31.4s

    The mallocBlocks=739241 looks suspicious to me, I tried to run audit via Instruments and this is the stack trace of the Malloc that has over 1GB

    realloc
    malloc_type_realloc_outlined
    __DataStorage.ensureUniqueBufferReference(growingTo:clear:)
    __DataStorage.resetBytes(in:)
    Data.InlineSlice.count.setter
    Data.Representation.count.setter
    AVIOReader.appendPersistentData(
    :generation:)
    PersistentReadDelegate.urlSession(
    :dataTask:didReceive:)
    @objc PersistentReadDelegate.urlSession(
    :dataTask:didReceive:)
    __77-[__NSCFURLSessionDelegateWrapper dataTask:didReceiveData:completionHandler:]_block_invoke_3
    _dispatch_call_block_and_release
    _dispatch_client_callout
    _dispatch_lane_serial_drain
    _dispatch_lane_invoke
    _dispatch_root_queue_drain_deferred_wlh
    _dispatch_workloop_worker_thread
    _pthread_wqthread
    start_wqthread

  4. superuser404notfound commented on Jun 10, 2026

    @superuser404notfound
    Owner

    That Instruments trace was exactly what was needed, thank you. Root cause found and fixed in release 3.0.1 (https://github.com/superuser404notfound/AetherEngine/releases/tag/3.0.1).

    The persistent reader's sliding window is a Data that gets appended at the tail and trimmed at the front with removeFirst. It turns out Data.removeFirst only advances the slice's lower bound: the backing storage stays addressed from its origin, and the append-side growth reallocates it to fit the slice's ever-increasing upper bound. So while window.count held steady at ~20 MB, the single backing allocation grew with every byte ever streamed through the connection, which at your 80 Mbps is ~14 MB/s, matching both your Xcode gauge and the realloc stack you captured. Paused playback stops the stream, which is why it looked stable there. The trim now re-bases the window into fresh compact storage; a standalone repro of the pattern went from +513 MB footprint per 512 MB streamed to +9 MB flat.

    With this and the subtitle-pacing fix from 3.0.0, both halves of your report should be resolved. If you can retest your 87 GB file (with and without subtitles) on 3.0.1 or current main, I'd expect the memprobe's mallocMB to plateau after the first minute. I'll keep this open until you can confirm.

  5. RomanLiberda commented on Jun 10, 2026

    @RomanLiberda
    ContributorAuthor

    Nice 👍 It looks good to me now! Memory leak is gone.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions