|
| 1 | +--- |
| 2 | +title: ExpandableMemoryStream |
| 3 | +page_title: ExpandableMemoryStream |
| 4 | +description: Learn about the segmented, dynamically growing in-memory stream implementation used for large PDF processing scenarios. |
| 5 | +slug: radpdfprocessing-formats-and-conversion-pdf-expandablememorystream |
| 6 | +tags: pdf, memory, performance, stream, fixed, document, processing, dpl, format, expandablememorystream, large, file, size |
| 7 | +published: True |
| 8 | +position: 3 |
| 9 | +--- |
| 10 | + |
| 11 | +# ExpandableMemoryStream |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +ExpandableMemoryStream is a segmented in‑memory stream optimized for large or many parallel PDF operations. Instead of resizing one big array, it grows by adding fixed‑size blocks only when needed. This keeps allocations smaller and steadier, helps the GC, and maintains predictable performance as documents scale. Compared to MemoryStream (which resizes one contiguous array and copies data on expansion), this segmented approach adds fixed blocks without copying existing bytes, reducing large reallocations and LOH pressure for very large or unpredictable workloads. The block‑based design grows incrementally without moving existing bytes and supports very large content sizes while keeping allocation behavior stable under parallel load. |
| 16 | + |
| 17 | +## Why a Segmented Approach |
| 18 | + |
| 19 | +Large PDF generation often needs a temporary buffer. A normal contiguous array may reallocate and copy data multiple times as it expands, increasing CPU work, peak memory, and pressure on the Large Object Heap (LOH). Avoiding large contiguous allocations lowers fragmentation, reduces garbage collection pauses, and scales better when size is unpredictable or workloads are bursty. |
| 20 | + |
| 21 | +## How It Works |
| 22 | + |
| 23 | +Data lives in equal‑sized blocks held in order. When more space is required a single new block is allocated, earlier blocks stay untouched. A position maps to (block index, offset). Growing exposes cleared bytes ready for writing. Shrinking lowers only the visible length and retains the blocks so later growth can reuse already allocated memory without new large allocations. |
| 24 | + |
| 25 | +## When to Use |
| 26 | + |
| 27 | +Use it when you need to: |
| 28 | + |
| 29 | +- Build or merge large PDFs fully in memory before saving. |
| 30 | +- Combine many pieces where the final size is unknown. |
| 31 | +- Run multiple document builds in parallel and want steady, predictable allocations. |
| 32 | +- Seek and rewrite parts of the buffered content without triggering array growth copies. |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +The following example shows two common ways to load a large PDF document into memory before further processing. The first approach constructs the stream directly from a byte array and passes an explicit segment size (bufferSize). The second approach creates an empty instance and copies a file stream into it. The constructor's second parameter (bufferSize) is optional and defaults to 1,000,000 bytes (1 MB). You can omit it unless you want a different segment size. |
| 37 | + |
| 38 | +<snippet id='libraries-pdf-formats-and-conversion-expandablememorystream-implementation'/> |
| 39 | + |
| 40 | +In both cases the segmented internal structure avoids reallocating a single large contiguous buffer, helping performance and memory stability for very large PDF files. |
| 41 | + |
| 42 | +## See Also |
| 43 | + |
| 44 | +* [PdfFormatProvider]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}) |
| 45 | +* [RadFixedDocument]({%slug radpdfprocessing-model-radfixeddocument%}) |
0 commit comments