Skip to content
English
  • There are no suggestions because the search field is empty.

IronBarCode Performance: High Peak Memory During ReadPdf (Large Multi-page PDFs)

Overview

Calling BarcodeReader.ReadPdf() once for an entire large PDF requires every requested page to be rendered for scanning at the configured DPI, so peak memory scales with the page count and resolution rather than staying flat. On a 248-page, 80MB file at 300 DPI this pushed memory between 3GB and 8GB, with spikes to 12GB — enough to fail in memory-limited environments. Reading the document in fixed page chunks bounds how many pages are in memory at any moment and removes the spike.


Environment

  • OS: Windows
  • Tested Version: 2026.4.2

Cause

BarcodeReader.ReadPdf() processes every page passed 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 must be held in memory together, so peak usage grows with the size of the page set. This is a function of how the read is batched, not a confirmed defect in IronBarCode.


Solution

  1. Recommended — Read barcodes in fixed page chunks using PdfBarcodeReaderOptions.PageNumbers, so only one chunk of pages is in memory at a time. A smaller chunkSize lowers peak memory but adds more passes — tune it to the environment's memory ceiling.
    var barcodeMetadata = new List();
    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}");

     

  2. Cache the PDF stream once before the loop and reset its position per chunk. The Stream property may allocate on each access, so read it into a local (var pdfStream = pdf.Stream;) and call pdfStream.Seek(0, SeekOrigin.Begin) before each chunk read.
  3. Build PdfBarcodeReaderOptions once, outside the loop. Change only PageNumbers per chunk instead of allocating a new options object every iteration.
  4. Pre-size the results list. Initialize it with an estimated capacity (the repro estimates roughly one barcode per ten pages) to avoid repeated internal array doubling as results are added.