IRONSOFTWAREHOME

High Peak Memory During ReadPdf on Large PDFs

Curtis Chau
Curtis Chau
Updated: June 24, 2026

Calling BarcodeReader.ReadPdf() once for an entire large PDF renders every requested page for scanning at the configured DPI, so peak memory scales with page count and resolution instead of staying flat. On a 248-page, 80MB file at 300 DPI, memory ran between 3GB and 8GB with spikes to 12GB, enough to fail in memory-limited environments.

BarcodeReader.ReadPdf() processes every page handed to it in a single operation. When the full page range of a large, high-DPI document is read at once, all of those pages are held in memory together, so peak usage grows with the size of the page set. This reflects how the read is batched, not a confirmed defect in IronBarcode.

Solution

1. Read in fixed page chunks

Split the document into fixed page chunks with PdfBarcodeReaderOptions.PageNumbers so only one chunk is in memory at a time. A smaller chunkSize lowers peak memory but adds more passes, so tune it to the environment's memory ceiling.

var barcodeMetadata = new List<BarcodeResult>();
var pageChunks = Enumerable.Range(1, pdf.PageCount)
    .GroupBy(i => i / 50)
    .Select(g => g.ToArray())
    .ToList();
foreach (var chunk in pageChunks)
{
    var readerOptions = new PdfBarcodeReaderOptions
    {
        ExpectMultipleBarcodes = true,
        ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional,
        Speed = ReadingSpeed.Detailed,
        ScanMode = BarcodeScanMode.OnlyDetectionModel,
        DPI = 300,
        PageNumbers = chunk
    };
    Console.WriteLine($"Processing pages: Pages {chunk.First()}-{chunk.Last()}");
    var chunkResult = BarcodeReader.ReadPdf(pdf.Stream, readerOptions);
    if (chunkResult != null && chunkResult.Count > 0)
    {
        barcodeMetadata.AddRange(chunkResult);
    }
    // Optional memory pressure relief
    GC.Collect();
    GC.WaitForPendingFinalizers();
}
Console.WriteLine($"Total barcodes found: {barcodeMetadata.Count}");
C#

Each iteration reads only the pages named in PageNumbers, so memory drops back between chunks rather than accumulating across the whole file.

2. Cache the PDF stream and reset its position

Read the stream into a local once before the loop, then rewind it per chunk. The Stream property may allocate on each access, so store it in var pdfStream = pdf.Stream; and call pdfStream.Seek(0, SeekOrigin.Begin) before each chunk read.

3. Build the reader options once

Construct PdfBarcodeReaderOptions outside the loop and change only PageNumbers per chunk, rather than allocating a fresh options object every iteration.

4. Pre-size the results list

Initialize the results list with an estimated capacity to avoid repeated internal array doubling as results are added. The repro estimates roughly one barcode per ten pages as a starting figure.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 2,422,100Version:2026.9just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronBarCode"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

  1. Download and unzip IronBarCode to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronBarCode.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required