Skip to content

Reduce remote memory use during large file uploads - #105

Open
pcai wants to merge 1 commit into
mainfrom
fix/large-upload-server-memory
Open

pcai wants to merge 1 commit into
mainfrom
fix/large-upload-server-memory

Conversation

@pcai

@pcai pcai commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #55.

Each upload chunk allocated two large objects on the remote host: the
base64 script text and the byte array from [Convert]::FromBase64String.
Both land on the Large Object Heap. The heap only drains on a Gen2
collection, which may not run during a long upload. A 200MB file can
therefore exhaust the remote PowerShell host's memory.

This change makes two fixes.

  • It decodes each base64 chunk straight into the file stream through a
    CryptoStream. No per-chunk byte array is allocated.
  • It runs [System.GC]::Collect() every 50 chunks. This bounds the
    remaining per-chunk garbage (script text and compiled script blocks).

The change keeps the existing chunked upload and the script block cache
clear. It drops no behavior.

This needs a real large-file upload test against a Windows host before
merge.

Decode each base64 chunk straight into the remote file stream through a
CryptoStream instead of allocating a per-chunk byte array with
[Convert]::FromBase64String. Each array landed on the Large Object Heap and
piled up between Gen2 collections, exhausting the remote host's memory
(#55). Also force a garbage collection every 50 chunks so the
remaining per-chunk LOH garbage (unique script text and ScriptBlocks) cannot
accumulate over a long upload. Disposing the StreamWriter flushes the final
base64 block and closes the whole stream chain.

This branch has not been deployed

No deployments
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.

System.OutOfMemoryException when uploading 200MB file

1 participant