Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

64.6 KB
Raw Blame
"""Release what the PDF libraries cache between the pages of a text walk.

Both PDF libraries keep, for the life of the document, everything they
decode or parse while extracting a page, and closing a pdfplumber page
does not release it:

- decoded streams. pdfminer (under pdfplumber) stores a stream's
  decompressed bytes on the ``PDFStream`` object (``.data``), which its
  object cache and the page objects keep reachable; pypdf stores a
  decoded copy on each ``EncodedStreamObject`` (``.decoded_self``),
  which its reader's object cache keeps reachable. A crafted page whose
  content stream is 20 MB of spaces compresses to ~20 KB, yields no
  characters and costs ~0.17 s of CPU, so a walk to the page ceiling
  held ~10 GB from a ~10 MB file.
- parsed objects. Every dictionary and array a page resolves (fonts,
  their ``/Widths``, resources) stays in the object cache, and a
  pdfminer font object keeps its own copy of the widths. A page whose
  font points at a 500,000-entry ``/Widths`` array held ~60 MB more per
  page, at ~2 s of CPU per page.
- font tables. pdfminer builds every font in a page's resources (and
  in a Form XObject's, on every ``Do`` that invokes it), with tables it
  expands from short ranges: a ``/ToUnicode`` CMap's ``<0000> <FFFF>
  <0000> beginbfrange`` becomes a 65,536-entry dictionary, and every
  font builds its own copy even when they all point at one ~100-byte
  compressed CMap stream. A font that is an object of its own is kept
  in the resource manager's font cache; one written inline in a
  resource dictionary is built again on every use and cached nowhere.
  Either way the parser that builds each map keeps it, and the decoded
  CMap bytes, in a reference cycle, so dropping the font does not free
  them, and parsing them creates too few tracked objects for the
  automatic collector to run often. Twenty pages of five such fonts held
  ~840 MB, from an 18 KB file or, with the fonts inline, a 13 KB one.
  Each font also builds its own widths table, copying the /Widths array
  even when many fonts share one (six fonts a page sharing one
  200,000-entry array grew ~97 MB a page); a Type 1 font without
  /Encoding hands a fresh copy of its /FontFile's first /Length1 bytes
  to a header parser that keeps it in a reference cycle (three fonts a
  page sharing a 20 MB /FontFile of spaces, a ~30 KB file, grew ~57 MB
  a page); the tokens a crafted /ToUnicode CMap or font header leaves
  on its parser's stack hold up to ~170 bytes per decoded byte, in the
  same cycle; and a CID font joins its /CIDSystemInfo /Registry and
  /Ordering into a new string of its own (five fonts a page sharing a
  20 MB /Registry string, a 25 KB file, grew ~95 MB a page).

A walker creates one releaser per document and calls ``before_page()``
just before each page's ``extract_text()`` and ``after_page()`` once the
page is done. A lazy pdfplumber walk (``utilities/pdf_page_walk``) calls
``before_page()`` before it builds each page instead, since building a
page resolves (and caches) its resources and contents.
``after_page()`` estimates what the library's caches hold
since the last release -- decoded stream bytes, plus
``_BYTES_PER_PARSED_ELEMENT`` for every object the walk has parsed and
for every element nested in it (a stream's dictionary included, read
without decoding the stream: a Form XObject's can hold any number of
entries), including the objects pdfminer parses
out of object streams (each counted once, whether or not a page has
resolved it through the object cache), and for every font pdfminer's
resource manager builds or hands out and every element nested in the
tables a build stores on the font (``_font_tables()``; a font or a
table seen again is counted once, but see below), with every string
among all of these counted by its size, plus the decoded bytes each
build feeds to a parser it leaves in a reference cycle
(``_parser_input_bytes()``) -- and, once that reaches
``MAX_PDF_RETAINED_DECODED_BYTES``, releases it: decoded streams go back
to their encoded state (the next access decodes them again from their
raw bytes, so a stream a later page shares still extracts correctly),
and every object the walk added to the cache is dropped, to be parsed
again from the file if a later page needs it, and the fonts are
dropped (the font cache cleared, the releaser's own references let
go), to be built again, and the reference cycles pdfminer's parsers
leave are collected. Objects cached before the first ``before_page()``
(the page tree, when the walk materialised it up front) are kept.
Honest documents stay far below the threshold and are left alone.

That estimate counts what the cases found so far showed a page can
keep, and each case found later (fonts written inline, widths copied
per font, font header copies, the strings a CID font joins) went
unbounded until it was counted. So the release also has a measured
trigger: where the process's resident memory can be read
(``/proc/self/statm``, on Linux), ``after_page()`` also releases once it
has grown by the threshold since the last release (or since the walk
began), whatever the estimate says, and the pdfminer releaser runs a
collection in the middle of a page once it has grown by the threshold
since the last collection. It is the whole process's memory: growth
that other threads cause, or that a release cannot free (pdfminer keeps
every name it parses in a process-wide table), makes a release or a
collection that frees nothing, after which the baseline is where the
memory then is, so the next one waits for another threshold's worth of
growth. On other platforms only the estimate applies. Of the eight
honest documents measured (up to 500 pages) it released one, once: a
one-page form whose three embedded TrueType fonts grew the process by
~69 MB.

Fonts are counted where they are built, not read off the font cache,
which misses the inline ones: the releaser wraps this document's
``rsrcmgr.get_font`` (an instance attribute, holding the manager and
the releaser weakly) and counts every font it returns, once per font
object, remembering the fonts weakly so that it keeps none pdfminer
frees. To count a table several fonts share only once, it holds the
tables it has counted, and lets go of those nothing else holds once
the ones added since it last did reach the threshold's worth, after
every collection and on a release (a table seen again after that is a
new object, and counts in full); so it can keep up to about the
threshold's worth of tables that pdfminer would have freed sooner, such
as the widths of a CID font written inline in a Form XObject, built
again on every ``Do``, plus those that something else held when it last
let go (such a table weighs once more toward the next time, so it goes
soon after its holder, the font being built, lets go of it). The tables
something else still holds it keeps, and never counts or walks again: a build stores some parts of the parsed font as
they are (the descriptor, a CID font's /CIDSystemInfo, the items of a
Type 3 font's /FontMatrix), and walking those on every ``Do`` of a form
with an inline font cost more than the build (a page of 100 ``Do``s of
a form whose inline font's descriptor holds 600,000 empty arrays took
~66 s of CPU, and ~130 s as a Type 3 font's /FontMatrix item, against
~3 s without the releaser; now ~6.3-6.7 s, the extra being the one
release, which parses the form again to rewind it, and its
collection). A nested container that another holds too is counted as
a table of its own, for the same reason. When it lets go of tables it
also asks for a full garbage collection, as it does once the
bytes fed to parsers since the last one, times
``_PARSER_BYTES_PER_INPUT_BYTE``, reach the threshold: what those
parsers, and the fonts pdfminer has dropped, hold in reference cycles
is garbage as soon as a build returns, so it is freed in the middle of
a page too (a page invoking a form of five inline 65,536-entry
/ToUnicode fonts 20 times took the process to ~880 MB before, ~110 MB
with it). Those bytes count only where pdfminer
parses them, but the factor is the worst case (a whitespace input
leaves nothing), and a collection costs time in proportion to every
object the process holds, whatever it frees. So a collection asked for
in the middle of a page waits until the walking thread has spent
``_COLLECT_SPACING`` (9) times the last collection's duration since it
ended -- such collections then take at most about a tenth of its CPU
time -- unless the resident memory has grown by ``_FORCED_GROWTH`` (4)
times the threshold since the last one; a release that only the
resident memory asks for waits the same way, and a release the
estimate asks for drops the caches at once but defers its collection
the same way (until a later font build or page, or the walk's end) --
except that a collection runs at once, however recent the last one,
once the garbage that only a collection frees, released since the
last one, reaches the threshold: what those parsers were fed, which
each holds -- a fresh copy (a /FontFile header) at once, a stream's own
decoded bytes, once each, when the release rewinds the stream -- and
the tables of the fonts built with them (their /ToUnicode map, which
the parser's cycle holds) once those fonts are let go of -- an inline
font's when its build returns, a cached font's at the release. Spaced
too, as in the previous revision, those were held for as long as
nine full collections of the process's heap took: 40 pages of five
fonts sharing a 65,536-entry /ToUnicode map peaked ~400 MB above the
start with 8 million objects held (~430 MB without the measured
trigger), and now 51-74 MB, as without objects held. What a deferred
collection would free besides is the part of the parser residue the
factor counts beyond each parser's input, which stays held until it
runs, for longer the more objects the process holds (on Linux at most
until the resident memory has grown by four times the threshold); one
still pending when the walk ends runs then (``close()``, or once the
releaser is garbage), and honest documents defer none. Before the
spacing, a page of
2,000 ``Do``s of a form with one inline Type 1 font whose /FontFile
has a /Length1 of 0 (a 1.3 KB file) ran 2,001 collections: ~141 s of
CPU instead of ~0.45 s, with a million objects held; it now runs none,
and with a /Length1 of the whole 400 KB of whitespace (which the
factor over-counts) 11 instead of 2,001 (~6.2 s instead of ~191 s,
against ~4.7 s without the releaser). And 100 pages whose font decodes
a shared 64 MB /FontFile of zeros and parses none of it (a 92 KB file),
which releases on every page, ran 101 collections with a million
objects held (~25 s of CPU) and now 25 (~20 s; the rest is decoding
the stream again on every page). Not bounded here: the names and
keywords pdfminer interns, in process-wide tables
(``psparser.PSLiteralTable`` and ``PSKeywordTable``) that it never
clears and compares against by identity, so the releaser cannot drop
them -- a 2 MB PDF of a million distinct names or keywords adds
~175 MB to the process for good, and each upload of new ones adds
more; what
one font build allocates before it returns (a /W range, a bfrange or
a crafted parser input expands within that build), and what the fonts
pdfminer still holds (cached ones, a form's while it runs) keep until
the release after the page.

Each ``after_page()`` scans the document's object cache, so it costs
time in proportion to that cache: well under a second over a whole
500-page honest document, ~0.16 s per page for a cache of a million
objects, which takes a page tree of about a million pages (itself an
O(all pages) cost paid before the walk). A pdfminer release also runs a
full garbage collection (pdfminer's parsers leave what they built in
reference cycles), which costs time in proportion to the objects the
whole process holds: ~0.06 s per million, and so does each collection
the font counting runs between releases; the spacing above keeps all
of them to about a tenth of the walking thread's CPU time (apart from
those a growth of four times the threshold forces, and those the
threshold's worth of garbage only a collection frees forces, which
cost builds of that size first). On the honest
documents measured (eight, up to 500 pages) only the one release above
ran. That time is spent in the walking thread, between the CPU budget's checks, and counts
against it.

These are library internals, written against the pdfplumber,
pdfminer.six and pypdf versions ``pdm.lock`` pins. Every access is
guarded: if an attribute is missing or anything raises, the releaser
stops releasing for that document and extraction carries on
unchanged (except the lookups of the streams a font build read, which
the document's data can make fail: one that fails counts nothing, as
the build could not have read that stream either; after a build that
raised they are looked up in the object cache only, which parses
nothing and follows no reference cycle), and the pdfminer
releaser removes its ``get_font`` wrapper and lets go of the font
tables it holds. The first time that happens to
each library's releaser in
a process (so at most two warnings) it is logged at WARNING, since the
memory bound is then off; later occurrences are logged at DEBUG.
pdfminer's object-stream cache entries are read as the ``(objects,
count)`` pairs it stores, taken in insertion order; an entry of another
shape is counted whole if it is a container (an over-count, which
brings the release sooner), and otherwise switches the releaser off. The canary tests in
``tests/research_library/services/test_pdf_extraction_bounds.py``
(``TestLibraryInternalsTheReleaseReliesOn``) fail if a library upgrade
removes what the releasers rely on, so an upgrade cannot undo them
silently.
"""

from __future__ import annotations

import gc
import os
import sys
import threading
import time
import weakref
from itertools import islice
from typing import Any

from loguru import logger

from . import pdf_extraction_limits

# Estimated bytes a parsed object element (an array item, a dictionary
# entry) keeps alive. Measured on a 500,000-entry /Widths array: ~124
# bytes per entry in pypdf (a boxed number object plus its list slot),
# and in pdfminer the cached array plus one font's own widths table
# together, which are now each counted (an over-count, which brings the
# release sooner).
_BYTES_PER_PARSED_ELEMENT = 128


# Bytes a pdfminer parser that a font build leaves in a reference cycle
# can keep per byte it was fed: the tokens a ToUnicode CMap or a Type 1
# font header leaves on the parser's stack. Measured on pdfminer.six
# 20260107 (CMapParser and Type1FontHeaderParser alike): ~52 for "1 1 1",
# ~84 for "<<<<", ~168 for "[[[[" or "{{{{", 0 for whitespace. This is
# the worst case, so it over-counts most inputs (all of a whitespace
# one); what that over-count can cost is bounded by the spacing of the
# collections it asks for (_COLLECT_SPACING).
_PARSER_BYTES_PER_INPUT_BYTE = 192

# A collection that the threshold asks for in the middle of a page waits
# until the walking thread has spent this many times the last
# collection's duration since it ended, so that those collections take
# at most about a tenth of the thread's CPU time (a full collection
# costs ~0.06 s per million objects the process holds, whatever it
# frees) -- unless the process's resident memory has grown by
# _FORCED_GROWTH times the threshold since the last one.
_COLLECT_SPACING = 9
_FORCED_GROWTH = 4

# How often, in seconds of the walking thread's CPU time, a font build
# reads the process's resident memory (~10 us a read).
_RSS_CHECK_INTERVAL = 0.01

_STRINGS = (str, bytes, bytearray)

_rss_unavailable = False


def _resident_bytes() -> int | None:
    """The process's resident memory in bytes, read from
    ``/proc/self/statm``; None where there is no such file (not Linux)
    or it cannot be read."""
    global _rss_unavailable
    if _rss_unavailable:
        return None
    try:
        with open("/proc/self/statm", "rb") as statm:
            pages = int(statm.read().split()[1])
        return pages * os.sysconf("SC_PAGE_SIZE")
    except (FileNotFoundError, AttributeError, ValueError, IndexError):
        # Not Linux (or not a statm we know): measure nothing from now on.
        _rss_unavailable = True
    except OSError:
        pass
    return None


def _measured(previous: int | None) -> int | None:
    """A new resident-memory baseline: the memory now, or *previous* if
    it cannot be read this time (a transient failure must not leave the
    baseline unknown, which would switch the measured trigger off)."""
    now = _resident_bytes()
    return previous if now is None else now


def _font_tables(font: object) -> list[object]:
    """The values a pdfminer font build stored that ``_font_built()``
    counts: every dictionary, list, tuple and string (``str`` or
    ``bytes``, by its size) held in an attribute of the font or of an
    object held in one (its ``unicode_map``, its ``cmap``), streams
    aside (their decoded bytes count where they are cached).

    That is the ``/ToUnicode`` map (``cid2unichr``) and the values its
    ``def``s set (``attrs``), the encoding's code-to-unicode table
    (``cid2unicode``, a copy per font when /Differences or the embedded
    font program gives one), the ``widths`` (simple and CID fonts alike:
    each build makes its own, with every array a width points at copied,
    even when many fonts share one /Widths array), a CID font's
    ``disps``, the descriptor, the bounding box, the font matrix, and
    strings such as a CID font's ``cidcoding``, which each build makes
    by joining its /CIDSystemInfo /Registry and /Ordering, however long
    those strings are and however many fonts share them. Every element
    nested in a container counts like a parsed element (a /ToUnicode
    map measured ~1.05x the estimate), and every string nested in one
    by its size as well. A value several fonts share (pdfminer's
    built-in encodings and metrics, a predefined CMap, a font
    descriptor) is counted once, and not again while something else
    holds it (``PdfplumberWalkReleaser._let_go_of_tables()``).
    """
    from pdfminer.pdftypes import PDFStream

    counted = _CONTAINERS + _STRINGS
    found = []
    held = []
    for value in getattr(font, "__dict__", {}).values():
        if isinstance(value, counted):
            found.append(value)
        elif hasattr(value, "__dict__") and not isinstance(
            value, (PDFStream, type)
        ):
            held.append(value)
    for owner in held:
        found.extend(v for v in vars(owner).values() if isinstance(v, counted))
    return found


def _cached_value(value: object) -> object:
    """*value*, or, if it is a reference, what it resolves to in its
    document's object cache, without parsing anything: a reference that
    is not cached, or that leads back to one already followed (``8 0
    obj 8 0 R``), resolves to None."""
    from pdfminer.pdftypes import PDFObjRef

    followed = set()
    while isinstance(value, PDFObjRef):
        if value.objid in followed:
            return None
        followed.add(value.objid)
        cache = getattr(value.doc, "_cached_objs", None)
        entry = cache.get(value.objid) if isinstance(cache, dict) else None
        if not isinstance(entry, tuple) or not entry:
            return None
        value = entry[0]
    return value


def _parser_input_bytes(
    spec: object, built: bool = True, inputs: list | None = None
) -> int:
    """Bytes the build of a font from *spec* fed to a pdfminer parser
    that it leaves in a reference cycle, under the conditions pdfminer
    parses them (``PDFResourceManager.get_font()`` picks the class from
    ``/Subtype``):

    - the decoded ``/ToUnicode`` CMap (``CMapParser``): resolved by a
      simple font (Type 1, TrueType, Type 3, or an unknown subtype); by a
      CID font only if it is a stream object itself rather than a
      reference (a Type 0 font hands its descendant the resolved one); a
      Type 0 font parses nothing itself, its descendant's build does;
    - for a Type 1 or TrueType font without ``/Encoding`` whose
      ``/BaseFont`` is not one of the standard fonts pdfminer has
      built-in metrics for (whose descriptor replaces the spec's), the
      copy of its decoded ``/FontFile``'s first ``/Length1`` bytes that
      ``Type1FontHeaderParser`` reads: exactly the length of that slice.

    A stream counts only if it is decoded (the lookup decodes nothing),
    which it is after a build that parsed it; a lookup that fails counts
    nothing, as the build could not have read that stream either. After
    a build that returned (*built*), the references followed are ones
    the build resolved itself; after one that raised, it may not have
    got that far, so they are looked up in the object cache only
    (``_cached_value()``): what the build resolved is there, and nothing
    is parsed, nor followed round a cycle.

    With *inputs*, each stream's decoded bytes the count includes are
    appended to it with the number of them fed, ``(data, fed)``: the
    parser holds *data* itself when it was fed all of it (pdfminer
    wraps it in a ``BytesIO``, which shares it), and a fresh copy of
    the first *fed* bytes otherwise."""
    from pdfminer.pdffont import FontMetricsDB
    from pdfminer.pdftypes import PDFStream, resolve1
    from pdfminer.psparser import PSLiteral, literal_name

    if not isinstance(spec, dict):
        return 0
    resolve = resolve1 if built else _cached_value

    def decoded(stream: object) -> bytes | None:
        if isinstance(stream, PDFStream) and stream.data is not None:
            return stream.data
        return None

    def resolved(holder: dict, key: str) -> object:
        try:
            return resolve(holder.get(key))
        except Exception as exc:
            logger.debug("font {} lookup failed ({})", key, type(exc).__name__)
            return None

    subtype = spec.get("Subtype")
    subtype = (
        literal_name(subtype)
        if isinstance(subtype, PSLiteral)
        else ("Type1" if subtype is None else None)
    )
    if subtype == "Type0":
        return 0
    if subtype in ("CIDFontType0", "CIDFontType2"):
        data = decoded(spec.get("ToUnicode"))
        if data is None:
            return 0
        if inputs is not None:
            inputs.append((data, len(data)))
        return len(data)
    data = decoded(resolved(spec, "ToUnicode"))
    total = len(data) if data is not None else 0
    if data is not None and inputs is not None:
        inputs.append((data, total))
    if subtype == "Type3" or "Encoding" in spec:
        return total
    basefont = spec.get("BaseFont")
    if isinstance(basefont, PSLiteral):
        try:
            FontMetricsDB.get_metrics(literal_name(basefont))
            return total  # built-in metrics: the spec's descriptor unused
        except KeyError:
            pass
        except Exception:
            return total  # the build raised before reading the font file
    descriptor = resolved(spec, "FontDescriptor")
    if not isinstance(descriptor, dict) or "FontFile" not in descriptor:
        return total
    font_file = resolved(descriptor, "FontFile")
    data = decoded(font_file)
    if data is None or "Length1" not in font_file.attrs:
        return total
    # pdfminer's int_value(): anything but an int is 0.
    length1 = resolved(font_file.attrs, "Length1")
    if not isinstance(length1, int):
        return total
    # The parser reads data[:length1], whatever the sign of /Length1.
    header = len(range(len(data))[:length1])
    if header and inputs is not None:
        inputs.append((data, header))
    return total + header


# Libraries whose releaser has already warned that it disabled itself in
# this process; later disables are logged at DEBUG only.
_warned_disabled: set[str] = set()
_warned_lock = threading.Lock()


def _log_disabled(library: str, exc: BaseException | None) -> None:
    """Log a releaser disabling itself: once per library at WARNING."""
    reason = type(exc).__name__ if exc is not None else "unknown"
    with _warned_lock:
        first = library not in _warned_disabled
        _warned_disabled.add(library)
    if first:
        logger.warning(
            "{} cache release disabled ({}): memory held between PDF pages "
            "is no longer bounded. The library internals it relies on may "
            "have changed; run tests/research_library/services/"
            "test_pdf_extraction_bounds.py. Further occurrences are logged "
            "at DEBUG.",
            library,
            reason,
        )
    else:
        logger.debug(
            "{} cache release disabled for this document ({})",
            library,
            reason,
        )


def _limit() -> int:
    return pdf_extraction_limits.MAX_PDF_RETAINED_DECODED_BYTES


_CONTAINERS = (dict, list, tuple)


def _sole_holder_refcount() -> int:
    """What ``sys.getrefcount()`` reports, read the way ``_count_elements()``
    and ``_let_go_of_tables()`` read it (on a loop variable), for a value
    that one container holds and nothing else (3 on CPython 3.12, 2 on
    3.14, whose loops borrow the reference); a value something else
    holds too reads more."""
    counts = [sys.getrefcount(value) for value in [[]]]
    return counts[0]


_SOLE_HOLDER = _sole_holder_refcount()

# Passes _let_go_of_tables() makes at most (see there).
_LET_GO_PASSES = 4


def _count_elements(
    obj: object, skip: tuple[type, ...], shared: list | None = None
) -> int:
    """Number of items in the lists and dicts nested in *obj*, plus, for
    every string (``str`` or ``bytes``) among them, among the dicts'
    keys, or *obj* itself, its size in units of
    ``_BYTES_PER_PARSED_ELEMENT`` (none for a short one, which its item
    already accounts for). Keys count because pdfminer turns a name that
    is not UTF-8 into the text of its repr, about four characters a
    byte.

    Objects of the *skip* types nested in *obj* (streams, whose data is
    accounted by its decoded bytes, and whose dictionary is counted
    where the stream is cached) are not descended into; *obj* itself is
    counted whatever its type, so a pypdf stream, which is its own
    dictionary, counts its entries (a pdfminer stream's dictionary is
    its ``attrs``). A dictionary is read through ``dict``'s own methods,
    which a subclass cannot override to resolve references. Parsed PDF
    objects are trees, so the walk is linear in their size -- no more
    than parsing them cost.

    With *shared*, a nested container that something besides its parent
    also holds is appended to *shared* instead of descended into, so
    that the caller counts it once, however many tables hold it: a font
    build can store a new container that holds parts of a parsed object
    (a Type 3 font's ``matrix`` is a new tuple of its /FontMatrix
    items), and walking those parts again on every build would cost more
    than the build did.
    """
    count = 0
    stack = [obj]
    while stack:
        item = stack.pop()
        if item is not obj and isinstance(item, skip):
            continue
        if isinstance(item, _STRINGS):
            count += sys.getsizeof(item) // _BYTES_PER_PARSED_ELEMENT
            continue
        if isinstance(item, dict):
            items: Any = dict.values(item)
            keys = dict.keys(item)
            if any(issubclass(t, _STRINGS) for t in set(map(type, keys))):
                count += sum(
                    sys.getsizeof(key) // _BYTES_PER_PARSED_ELEMENT
                    for key in keys
                    if isinstance(key, _STRINGS)
                )
        elif isinstance(item, (list, tuple)):
            items = item
        else:
            continue
        count += len(item)
        # A flat table (a font's widths, a /ToUnicode map) is told apart
        # from its item types alone, without a Python-level step per item.
        types = set(map(type, items))
        if any(issubclass(t, _CONTAINERS) for t in types):
            if shared is None:
                stack.extend(items)
                continue
            for value in items:
                if (
                    isinstance(value, _CONTAINERS)
                    and sys.getrefcount(value) > _SOLE_HOLDER
                ):
                    shared.append(value)
                else:
                    stack.append(value)
        elif any(issubclass(t, _STRINGS) for t in types):
            count += sum(
                sys.getsizeof(value) // _BYTES_PER_PARSED_ELEMENT
                for value in items
                if isinstance(value, _STRINGS)
            )
    return count


def _is_objstm_entry(value: object) -> bool:
    """Whether *value* has the shape pdfminer gives a ``_parsed_objs``
    entry: ``(objects, count)``, a list of the stream's header numbers
    and objects plus the number of objects."""
    return (
        isinstance(value, tuple)
        and len(value) == 2
        and isinstance(value[0], list)
        and isinstance(value[1], int)
    )


def _require_container(value: object) -> None:
    """Raise unless ``_count_elements`` can count *value*: an entry with
    nothing to count it by stops the releaser (``_disable()`` warns)
    rather than going uncounted."""
    if not isinstance(value, (dict, list, tuple)):
        raise TypeError("unexpected pdfminer object stream cache entry")


class _Pending:
    """What a pdfminer walk has left for the cycle collector: whether a
    collection is due (asked for, and deferred by the spacing), and an
    estimate, in bytes, of the garbage that only a collection frees,
    released since the last one (``PdfplumberWalkReleaser._font_parsed()``).
    Kept apart from the releaser so that the walk-end collection
    (``_collect_at_walk_end()``) can read it once the releaser is gone."""

    __slots__ = ("due", "garbage")

    def __init__(self) -> None:
        self.due = False
        self.garbage = 0


def _collect_at_walk_end(pending: _Pending) -> None:
    """Run the collection a walk deferred, if what it would free is
    garbage only a collection frees, so that the walk does not end
    holding it. Honest documents never ask for one."""
    if pending.due and pending.garbage > 0:
        pending.due = False
        pending.garbage = 0
        gc.collect()


class PdfplumberWalkReleaser:
    """Releases pdfminer's caches while pdfplumber walks a document."""

    def __init__(self, pdf: Any) -> None:
        self._pdf = pdf
        self._enabled = True
        self._pre_walk_keys: set | None = None
        self._page_mark: int | None = None
        # Set by before_page() alongside _page_mark (keep it there when
        # merging other before_page() changes): without it after_page()
        # counts every object stream parsed before the page, including
        # the pre-walk ones, against the threshold.
        self._objstm_mark: int | None = None
        self._parsed_elements = 0
        # Fonts counted since the last release, by id, held weakly: a
        # font handed out again (from pdfminer's cache, or as a Type0
        # font's descendant) is not counted twice, and holding the fonts
        # would keep ones pdfminer itself lets go of -- every unknown
        # font name in a Tf builds a new font that nothing caches.
        self._fonts_seen: weakref.WeakValueDictionary[int, Any] = (
            weakref.WeakValueDictionary()
        )
        # The font tables and long strings counted (by id, and held so
        # that no id is reused for a new one while it is here), so one
        # that several fonts share counts once; and the entries of those
        # added since the tables were last let go of (at least one per
        # table, so that empty tables cannot pile up). Holding them
        # keeps values pdfminer may already have let go of (an inline
        # CID font's widths die with the font), so once those entries
        # reach the threshold, and after every collection,
        # _let_go_of_tables() lets go of the tables nothing else holds;
        # the rest (a descriptor or a /FontMatrix item that a parsed
        # object holds, pdfminer's built-in tables) cost nothing to hold
        # and are kept, so that they are never walked again.
        self._table_ids: set[int] = set()
        self._tables: list[object] = []
        self._held_entries = 0
        # The entries of each table added since the tables were last
        # let go of, by id: one kept then (something else still held
        # it, such as the font being built) weighs again until the next
        # time, so that it is let go of soon after that holder is.
        self._new_tables: dict[int, int] = {}
        # Decoded bytes the font builds since the last release fed to
        # parsers left in reference cycles (_parser_input_bytes()),
        # counted against the threshold; and since the last collection
        # of those cycles.
        self._font_bytes = 0
        self._uncollected_bytes = 0
        # The measured side (_resident_bytes(); None where it cannot be
        # read): the process's resident memory after the last release
        # and after the last collection (or when the walk began), and
        # when (in the thread's CPU time) the last collection ended and
        # how long it took, which space the next one (_may_collect()).
        self._release_rss: int | None = None
        self._collect_rss: int | None = None
        self._collect_end = 0.0
        self._collect_cost = 0.0
        # Whether a collection is due, and the garbage only a collection
        # frees, released since the last one: the decoded bytes fed to
        # parsers (which those parsers keep in reference cycles once the
        # build returns) and the tables of the fonts built with them
        # (their /ToUnicode map, which the parser's cycle holds) once
        # those fonts are let go of -- an inline font's when its build
        # returns, a cached font's at the next release, when the font
        # cache is cleared. A collection runs at once, whatever the
        # spacing, when that garbage reaches the threshold
        # (_font_parsed(), after_page()). The tables of a build not
        # classified yet (a Type0 font's descendant, built inside it)
        # and of the cached fonts built since the last release:
        self._pending = _Pending()
        self._unclassified = 0
        self._cached_garbage = 0
        # The decoded streams fed whole to parsers since the last
        # release (their sizes, by the id of the bytes, which the stream
        # holds until the release rewinds it): garbage that only a
        # collection frees from then on.
        self._fed_streams: dict[int, int] = {}
        # A deferred collection still pending when the walk ends runs
        # then, when the releaser goes (or on close()).
        self._walk_end = weakref.finalize(
            self, _collect_at_walk_end, self._pending
        )
        self._walk_end.atexit = False
        # A release whose collection was deferred: the collection that
        # follows sets _release_rss.
        self._release_collect_due = False
        self._rss_checked = 0.0
        self._rsrcmgr: Any = None
        # ids of the objects counted from object-stream lists since the
        # last release. Every one stays reachable from _parsed_objs until
        # that release clears the set, so no id is reused for a different
        # object while it is here.
        self._counted: set[int] = set()

    def _count_fonts(self) -> None:
        """Wrap this document's ``rsrcmgr.get_font`` to count every font
        it returns, where it is built.

        pdfminer caches only fonts that are objects of their own
        (``_cached_fonts``, keyed by object number); a font dictionary
        written inline in a ``/Resources /Font`` dictionary -- a page's,
        or a Form XObject's, initialised on every ``Do`` -- is built again
        each time and cached nowhere, so the cache cannot show what a
        page built. Every font goes through ``get_font()``
        (``init_resources()``, an unknown font name in ``Tf``, and a
        Type0 font's descendant), which pdfminer calls on the instance.
        The wrapper is an instance attribute, so only this document's
        resource manager is affected, and it holds the manager and the
        releaser weakly: it adds no reference cycle, and once the
        releaser is gone or disabled it only passes calls through.
        """
        from pdfminer.pdfinterp import PDFResourceManager

        rsrcmgr = self._pdf.rsrcmgr
        if not isinstance(rsrcmgr, PDFResourceManager) or "get_font" in vars(
            rsrcmgr
        ):
            raise TypeError("unexpected pdfminer resource manager")
        cached = rsrcmgr._cached_fonts
        if not isinstance(cached, dict):
            raise TypeError("unexpected pdfminer font cache")
        build = type(rsrcmgr).get_font
        manager = weakref.ref(rsrcmgr)
        releaser = weakref.ref(self)
        # How many get_font() calls are running: a Type0 font builds its
        # descendant with a nested one, which is not cached, but lives
        # as long as the Type0 font does.
        depth = [0]

        def get_font(objid: object, spec: Any) -> Any:
            depth[0] += 1
            try:
                font = build(manager(), objid, spec)
            except BaseException:
                # A build that fails part-way may have parsed its
                # /ToUnicode already.
                owner = releaser()
                if owner is not None:
                    owner._font_parsed(spec, built=False, outer=depth[0] == 1)
                raise
            finally:
                depth[0] -= 1
            owner = releaser()
            if owner is not None:
                fonts = getattr(manager(), "_cached_fonts", None)
                owner._font_built(
                    font,
                    spec,
                    outer=depth[0] == 0,
                    cached=bool(objid)
                    and isinstance(fonts, dict)
                    and fonts.get(objid) is font,
                )
            return font

        # Fonts cached before the walk are not its cost.
        for font in cached.values():
            self._fonts_seen[id(font)] = font
        rsrcmgr.get_font = get_font
        self._rsrcmgr = rsrcmgr

    def _font_built(
        self,
        font: object,
        spec: object,
        outer: bool = True,
        cached: bool = False,
    ) -> None:
        """Count a font ``get_font()`` returned: one element, the
        elements of its tables not counted yet, and the bytes its build
        fed to parsers (``_font_parsed()``). A font handed out again
        (from the cache, or a Type0 font's descendant) was not built
        again. A table, or a container nested in one, that something
        else holds too is counted once, as a table of its own, and kept
        while that holder keeps it (``_let_go_of_tables()``): a build
        stores some parts of the parsed font as they are (the
        descriptor, a CID font's /CIDSystemInfo, the items of a Type 3
        font's /FontMatrix), and an inline font in a form is built again
        on every ``Do``, so walking them again would cost more than the
        build (a 600,000-entry descriptor took ~0.9 s a ``Do``). *outer*
        is False for a build nested in another (a Type0 font's
        descendant), and *cached* tells whether pdfminer cached the font
        (``_font_parsed()``). Never raises (it runs inside pdfminer's
        page interpretation)."""
        if not self._enabled:
            return
        try:
            from pdfminer.pdftypes import PDFStream

            if self._fonts_seen.get(id(font)) is font:
                if outer and self._unclassified:
                    self._font_parsed(None, outer=True, cached=cached)
                return
            self._fonts_seen[id(font)] = font
            self._parsed_elements += 1
            built = 0
            work = _font_tables(font)
            while work:
                table = work.pop()
                if id(table) in self._table_ids:
                    continue
                shared: list = []
                entries = _count_elements(table, (PDFStream,), shared)
                if isinstance(table, _STRINGS) and not entries:
                    continue  # short: the font's own element covers it
                self._table_ids.add(id(table))
                self._tables.append(table)
                self._held_entries += max(1, entries)
                self._new_tables[id(table)] = max(1, entries)
                self._parsed_elements += entries
                built += entries
                work.extend(shared)
            let_go = self._held_entries * _BYTES_PER_PARSED_ELEMENT >= _limit()
        except Exception as exc:
            self._disable(exc)
            return
        # What the fonts pdfminer has let go of (an inline font built on
        # every Do of a form) holds in reference cycles is now free to
        # collect, in the middle of the page; the collection lets go of
        # the tables that leaves only the releaser holding. If the
        # spacing defers it, they are let go of here all the same.
        collected = self._collect_end
        self._font_parsed(
            spec, collect=let_go, entries=built, outer=outer, cached=cached
        )
        if let_go and self._enabled and self._collect_end == collected:
            try:
                self._let_go_of_tables()
            except Exception as exc:
                self._disable(exc)

    def _let_go_of_tables(self) -> None:
        """Let go of the font tables held that nothing else holds (the
        ones pdfminer has let go of), and keep the others: holding them
        costs nothing, their ids stay theirs, and so they are not
        counted, nor walked, again. Called once the entries of the
        tables added since the last call reach the threshold's worth,
        after every collection (which frees the reference cycles that
        hold the tables of fonts pdfminer dropped) and on a release."""
        tables, self._tables = self._tables, []
        # A container only the list holds, read the way the loop below
        # reads the tables: if it reads more (an interpreter whose
        # references differ from the calibration), the counts cannot be
        # trusted, and every table is let go of -- counted again if seen
        # again, an over-count -- rather than kept while nothing holds it.
        probe = [[]]
        trusted = True
        for index in range(len(probe)):
            table = probe[index]
            trusted = sys.getrefcount(table) <= _SOLE_HOLDER
        table = probe = None
        if not trusted:
            tables = []
        # Each one let go of at once, so that a table held only by
        # another one (a nested container counted as a table of its own)
        # goes with it; one counted before its holder goes in the next
        # pass (pdfminer's builds nest them a level or two deep).
        kept: list = []
        for _ in range(_LET_GO_PASSES):
            kept = []
            for index in range(len(tables)):
                table = tables[index]
                if sys.getrefcount(table) > _SOLE_HOLDER:
                    kept.append(table)
                else:
                    tables[index] = None
            table = None
            if len(kept) == len(tables):
                break
            tables = kept
        self._tables = kept
        self._table_ids = {id(table) for table in kept}
        # A table kept for the first time weighs once more: what still
        # held it may be about to let go of it (the font being built, a
        # cycle not collected yet). One kept again weighs nothing.
        new, self._new_tables = self._new_tables, {}
        self._held_entries = sum(new.get(id(table), 0) for table in kept)

    def _font_parsed(
        self,
        spec: object,
        collect: bool = False,
        built: bool = True,
        entries: int = 0,
        outer: bool = True,
        cached: bool = False,
    ) -> None:
        """Count the bytes a font build from *spec* fed to parsers that
        pdfminer leaves in reference cycles, and collect those cycles
        once what they can hold reaches the threshold (or when
        *collect*), or once the process's resident memory has grown by
        the threshold since the last collection -- spaced by
        ``_may_collect()`` either way.

        The parser and what it was fed are garbage as soon as the build
        returns, but only the cycle collector frees them, and they can
        create too few tracked objects for it to run: a Type 1 font
        without /Encoding parses a fresh copy of its /FontFile's first
        /Length1 bytes, and three such fonts a page sharing a 20 MB
        /FontFile of spaces grew ~57 MB a page; the tokens a crafted
        /ToUnicode leaves on its parser's stack hold up to
        ``_PARSER_BYTES_PER_INPUT_BYTE`` times its size. Collecting here,
        not after the page, also bounds a page that builds many such
        fonts. *built* is False after a build that raised
        (``_parser_input_bytes()`` then parses nothing).

        Those collections are spaced (``_may_collect()``), because the
        factor is the worst case, except once the garbage that only a
        collection frees, released since the last one, reaches the
        threshold: what the parsers were fed, which each holds in its
        cycle -- a fresh copy (a /FontFile header) at once, a stream's
        own decoded bytes (which the stream holds too, and which several
        builds can share) once each, when the release rewinds the
        stream -- and the tables (*entries*, counted by
        ``_font_built()``) of a font whose build fed one, whose
        /ToUnicode map its parser's cycle holds, once the font is let go
        of: an inline font's (not *cached*) as soon as its outermost
        build (*outer*) returns, a cached font's at the next release.
        That collection runs at once: the garbage it frees cost
        builds of its size, and deferring it held what it frees for as
        long as a collection of the whole process's heap took, nine
        times over (40 pages of five fonts sharing a 65,536-entry
        /ToUnicode map, with 8 million objects held, peaked ~400 MB
        above the start). Never raises."""
        if not self._enabled:
            return
        try:
            pending = self._pending
            inputs: list = []
            fed = (
                _parser_input_bytes(spec, built, inputs)
                if spec is not None
                else 0
            )
            self._font_bytes += fed
            self._uncollected_bytes += fed
            for data, count in inputs:
                if count < len(data):
                    # A fresh copy (a /FontFile header): only the
                    # parser's cycle holds it.
                    pending.garbage += count
                else:
                    # The stream's own decoded bytes: the parser's cycle
                    # holds them once the release rewinds the stream.
                    self._fed_streams[id(data)] = count
            del inputs
            if fed:
                self._unclassified += entries * _BYTES_PER_PARSED_ELEMENT
            if outer:
                if cached:
                    self._cached_garbage += self._unclassified
                else:
                    pending.garbage += self._unclassified
                self._unclassified = 0
            if pending.garbage >= _limit():
                self._collect()
                return
            if (
                collect
                or self._uncollected_bytes * _PARSER_BYTES_PER_INPUT_BYTE
                >= _limit()
            ):
                pending.due = True
            now = time.thread_time()
            if not pending.due:
                # Reading the resident memory costs ~10 us: not on every
                # build of a page that builds thousands of fonts.
                if now - self._rss_checked < _RSS_CHECK_INTERVAL:
                    return
                self._rss_checked = now
            if self._collect_rss is None:  # not read yet: from now on
                self._collect_rss = _resident_bytes()
            grown = self._grown(self._collect_rss)
            if (pending.due or grown >= _limit()) and self._may_collect(
                now, grown
            ):
                self._collect()
        except Exception as exc:
            self._disable(exc)

    def _grown(self, baseline: int | None) -> int:
        """How far the process's resident memory is above *baseline*
        (0 when either is unknown)."""
        if baseline is None:
            return 0
        now = _resident_bytes()
        return 0 if now is None else now - baseline

    def _may_collect(self, now: float, grown: int) -> bool:
        """Whether a collection the threshold asks for may run at *now*
        (the thread's CPU time): once the thread has spent
        ``_COLLECT_SPACING`` times the last collection's duration since
        it ended, or at once if the resident memory has grown by
        ``_FORCED_GROWTH`` times the threshold (*grown*). A collection
        costs time in proportion to every object the process holds,
        whatever it frees, and what asks for it can be cheap: unspaced,
        a page of 2,000 ``Do``s of a form with one inline font took
        ~191 s of CPU instead of ~4.7 s, with a million objects held."""
        if grown >= _FORCED_GROWTH * _limit():
            return True
        return now - self._collect_end >= _COLLECT_SPACING * self._collect_cost

    def _collect(self) -> None:
        """Run a full collection, timing it and measuring the resident
        memory it leaves (the measure taken before stands if that read
        fails), then let go of the font tables it left only the
        releaser holding."""
        start = time.thread_time()
        gc.collect()
        self._collect_end = time.thread_time()
        self._collect_cost = self._collect_end - start
        self._pending.due = False
        self._pending.garbage = 0
        self._uncollected_bytes = 0
        self._collect_rss = _measured(self._collect_rss)
        if self._release_collect_due:
            self._release_collect_due = False
            self._release_rss = self._collect_rss
        self._let_go_of_tables()

    def _collect_if_due(self, now: float) -> None:
        """Run a collection asked for earlier (by a release that
        deferred it) once ``_may_collect()`` allows it."""
        if self._pending.due and self._may_collect(
            now, self._grown(self._collect_rss)
        ):
            self._collect()

    def _parts(self) -> tuple[Any, dict, dict, dict]:
        doc = self._pdf.doc
        cache = doc._cached_objs
        parsed_objstms = doc._parsed_objs
        fonts = self._pdf.rsrcmgr._cached_fonts
        if not (
            isinstance(cache, dict)
            and isinstance(parsed_objstms, dict)
            and isinstance(fonts, dict)
        ):
            raise TypeError("unexpected pdfminer cache layout")
        return doc, cache, parsed_objstms, fonts

    def before_page(self) -> None:
        """Call just before a page's ``extract_text()``, or, in a lazy
        walk, just before the page is built. Never raises."""
        if not self._enabled:
            return
        try:
            _doc, cache, parsed_objstms, _fonts = self._parts()
            if self._pre_walk_keys is None:
                # What is cached here was not added by the walk and is
                # never dropped. In a lazy walk the page-tree nodes are
                # parsed as the walk reaches them, so they are counted
                # and released like any other object the walk added.
                self._pre_walk_keys = set(cache)
                self._count_fonts()
                self._release_rss = self._collect_rss = _resident_bytes()
                self._collect_end = self._rss_checked = time.thread_time()
            self._page_mark = len(cache)
            # Required for after_page() to skip object streams parsed
            # before this page; see __init__.
            self._objstm_mark = len(parsed_objstms)
        except Exception as exc:
            self._disable(exc)

    def _estimate(self, cache: dict, parsed_objstms: dict) -> int:
        """Count what this page added to the caches and return what
        they hold since the last release, in bytes."""
        from pdfminer.pdftypes import PDFStream

        skip = (PDFStream,)
        # Object streams this page parsed (new keys are appended, so
        # they follow _objstm_mark in insertion order): pdfminer keeps
        # every object of such a stream, and its header numbers, in
        # one list apart from the object cache -- including objects
        # no page has resolved yet.
        for value in islice(
            parsed_objstms.values(), self._objstm_mark or 0, None
        ):
            if not _is_objstm_entry(value):
                _require_container(value)
                # Not the (objects, count) pair this was written
                # against: count all of it (an over-count, which
                # brings the release sooner).
                self._parsed_elements += 1 + _count_elements(value, skip)
                continue
            objs = value[0]
            self._parsed_elements += len(objs)
            for obj in objs:
                if isinstance(obj, (dict, list)):
                    self._counted.add(id(obj))
                    self._parsed_elements += _count_elements(obj, skip)
        # Objects the getobj() calls of this page appended to the
        # cache; each counts at least one element, so empty and scalar
        # objects are not free. An object resolved out of an object
        # stream is the one in that stream's list, already counted
        # there (on this page or an earlier one).
        for obj, _genno in islice(cache.values(), self._page_mark, None):
            self._parsed_elements += 1
            if isinstance(obj, PDFStream):
                # Its dictionary is parsed like any other (a Form
                # XObject's can hold any number of entries); its data
                # counts below, by its decoded size, and is not decoded
                # here.
                self._parsed_elements += _count_elements(obj.attrs, skip)
            elif id(obj) not in self._counted:
                self._parsed_elements += _count_elements(obj, skip)
        # Fonts were counted as get_font() returned them
        # (_count_fonts()).
        self._page_mark = None
        self._objstm_mark = None
        held = (
            self._parsed_elements * _BYTES_PER_PARSED_ELEMENT + self._font_bytes
        )
        for obj, _genno in list(cache.values()):
            if isinstance(obj, PDFStream) and obj.data is not None:
                held += len(obj.data)
        return held

    def after_page(self) -> bool:
        """Call once the page is done (after ``close()``). Returns True
        when a release ran. Never raises."""
        if not self._enabled or self._page_mark is None:
            return False
        try:
            from pdfminer.pdftypes import PDFStream

            doc, cache, parsed_objstms, fonts = self._parts()
            # In a method of its own so that none of the objects it
            # looks at stays referenced from this frame: the release
            # below lets go of the font tables nothing else holds.
            held = self._estimate(cache, parsed_objstms)
            now = time.thread_time()
            if self._release_rss is None:  # not read yet: from now on
                self._release_rss = _resident_bytes()
            measured = False
            if held < _limit():
                # What the estimate misses, the resident memory shows:
                # release once it has grown by the threshold since the
                # last release, spaced like the other collections.
                grown = self._grown(self._release_rss)
                if grown < _limit() or not self._may_collect(now, grown):
                    self._collect_if_due(now)
                    return False
                measured = True
            # Rewound in place rather than dropped: pages hold the streams
            # cached before the walk directly (PDFPage.contents), and a
            # finished page's content parser, which pdfminer leaves in a
            # reference cycle, still holds the streams it read.
            self._rewind_streams(doc, cache, PDFStream)
            # Everything the walk added is parsed again from the file if a
            # later page needs it.
            pre_walk = self._pre_walk_keys or set()
            for objid in list(cache):
                if objid not in pre_walk:
                    del cache[objid]
            # Objects parsed out of object streams are cached separately.
            parsed_objstms.clear()
            # Fonts keep their tables (counted as built) and embedded font
            # program (a TrueType font holds its decoded FontFile2);
            # rebuilt on demand. The fonts pdfminer did not cache
            # (inline ones) are held only by what the finished pages
            # left in reference cycles, and their tables by _tables
            # until the collection below lets go of them.
            fonts.clear()
            self._fonts_seen.clear()
            self._held_entries = 0
            self._parsed_elements = 0
            self._font_bytes = 0
            self._uncollected_bytes = 0
            self._counted.clear()
            # The cached fonts are let go of: the tables of those built
            # with parsers are now garbage that only a collection frees.
            self._pending.garbage += self._cached_garbage + sum(
                self._fed_streams.values()
            )
            self._cached_garbage = 0
            self._fed_streams.clear()
            # Dropping the fonts is not enough to free their /ToUnicode
            # maps: the CMapParser that built each one keeps it, and the
            # decoded CMap bytes, in a reference cycle (its bound-method
            # state), which only the cycle collector frees, and parsing
            # them creates too few tracked objects to trigger it. Without
            # this, a walk that released every second page still grew
            # ~840 MB over 20 pages. The collection is spaced like the
            # others: a release the estimate asks for can be cheap (a
            # 64 MB stream of zeros that a font build decodes and hardly
            # parses), and a collection costs time in proportion to every
            # object the process holds -- unless what it frees, the
            # garbage only a collection frees, has reached the threshold
            # (_font_parsed()): then it runs at once. A deferred one runs
            # once the spacing allows (at a later font build or page),
            # or once that garbage reaches the threshold, or when the
            # walk ends, and what it would free is held until then.
            if (
                measured
                or self._pending.garbage >= _limit()
                or self._may_collect(now, self._grown(self._collect_rss))
            ):
                self._collect()
                self._release_rss = self._collect_rss
            else:
                self._let_go_of_tables()
                self._pending.due = True
                self._release_collect_due = True
                self._release_rss = _measured(self._release_rss)
            return True
        except Exception as exc:
            self._disable(exc)
            return False

    @staticmethod
    def _rewind_streams(doc: Any, cache: dict, stream_type: type) -> None:
        """Return every decoded, cached ``PDFStream`` to its encoded state.

        pdfminer drops a stream's raw bytes once it has decoded them
        (``rawdata`` becomes ``None``), so they are read back by parsing
        the object from the file again. The cached object keeps its
        identity -- pages that hold it directly (``PDFPage.contents``)
        see the rewound object -- and only ``rawdata``/``data`` change.
        """
        for objid, entry in list(cache.items()):
            obj = entry[0]
            if not isinstance(obj, stream_type) or obj.data is None:
                continue
            if obj.rawdata is not None:
                continue  # not a state decode() leaves behind
            del cache[objid]
            try:
                fresh = doc.getobj(objid)
            except Exception:
                fresh = None
            finally:
                cache[objid] = entry
            if (
                fresh is not obj
                and isinstance(fresh, stream_type)
                and fresh.rawdata is not None
                and fresh.data is None
            ):
                obj.rawdata = fresh.rawdata
                obj.data = None

    def close(self) -> None:
        """Call once the walk is done (optional: it also runs when the
        releaser is garbage). Runs a collection a release or a font build
        deferred, if what it would free is garbage only a collection
        frees, so that the walk does not end holding it; honest
        documents never defer one. Never raises."""
        try:
            self._walk_end()
        except Exception as exc:
            logger.debug("walk-end collection failed ({})", type(exc).__name__)

    def _disable(self, exc: BaseException | None = None) -> None:
        self._enabled = False
        # Stop counting fonts and let go of the tables held: with the
        # release off they would otherwise be kept for the rest of the
        # walk.
        self._tables = []
        self._table_ids = set()
        self._new_tables = {}
        rsrcmgr, self._rsrcmgr = self._rsrcmgr, None
        if rsrcmgr is not None:
            try:
                del rsrcmgr.get_font
            except Exception:
                # The wrapper then only passes calls through.
                logger.debug("pdfminer get_font wrapper left in place")
        _log_disabled("pdfminer", exc)


class PypdfWalkReleaser:
    """Releases pypdf's caches while a ``PdfReader`` is walked."""

    def __init__(self, reader: Any) -> None:
        self._reader = reader
        self._enabled = True
        self._pre_walk_keys: set | None = None
        self._page_mark: int | None = None
        self._parsed_elements = 0
        # The process's resident memory after the last release (or when
        # the walk began); None where it cannot be read.
        self._release_rss: int | None = None

    def _cache(self) -> dict:
        cache = self._reader.resolved_objects
        if not isinstance(cache, dict):
            raise TypeError("unexpected pypdf cache layout")
        return cache

    def before_page(self) -> None:
        """Call just before a page's ``extract_text()``. Never raises."""
        if not self._enabled:
            return
        try:
            cache = self._cache()
            if self._pre_walk_keys is None:
                # The page tree was flattened to yield this page.
                self._pre_walk_keys = set(cache)
                self._release_rss = _resident_bytes()
            self._page_mark = len(cache)
        except Exception as exc:
            self._disable(exc)

    def after_page(self) -> bool:
        """Call once the page is done. Returns True when a release ran.
        Never raises."""
        if not self._enabled or self._page_mark is None:
            return False
        try:
            from pypdf.generic import EncodedStreamObject

            cache = self._cache()
            # Each new object counts at least one element, so empty and
            # scalar objects are not free; a stream counts the entries of
            # its dictionary (it is one) like any other, and its data by
            # its decoded size below.
            for obj in islice(cache.values(), self._page_mark, None):
                self._parsed_elements += 1 + _count_elements(
                    obj, (EncodedStreamObject,)
                )
            self._page_mark = None
            held = self._parsed_elements * _BYTES_PER_PARSED_ELEMENT
            decoded = []
            for obj in list(cache.values()):
                if isinstance(obj, EncodedStreamObject):
                    copy = obj.decoded_self
                    if copy is not None:
                        held += len(copy.get_data())
                        decoded.append(obj)
            if self._release_rss is None:  # not read yet: from now on
                self._release_rss = _resident_bytes()
            if held < _limit():
                # Release too once the resident memory has grown by the
                # threshold since the last release.
                now = _resident_bytes()
                if (
                    self._release_rss is None
                    or now is None
                    or now - self._release_rss < _limit()
                ):
                    return False
            # EncodedStreamObject keeps its raw bytes and decodes them
            # again when decoded_self is None.
            for obj in decoded:
                obj.decoded_self = None
            pre_walk = self._pre_walk_keys or set()
            for key in list(cache):
                if key not in pre_walk:
                    del cache[key]
            self._parsed_elements = 0
            self._release_rss = _measured(self._release_rss)
            return True
        except Exception as exc:
            self._disable(exc)
            return False

    def _disable(self, exc: BaseException | None = None) -> None:
        self._enabled = False
        _log_disabled("pypdf", exc)