Skip to content

Memory leak in phonemize.py #198

@Dave1475

Description

@Dave1475

Memory leak in phonemize.py

phonemizer = BACKENDS[backend]
Spawns a new instance each time it's ran, while the old one persists in memory

One solution is to cache it, so it's only instantiated once:

Global cache for phonemizer backends

_PHONEMIZER_CACHE = {}

Create a cache key from the configuration

cache_key = (
    backend,
    language,
    str(punctuation_marks),
    preserve_punctuation,
    with_stress,
    tie,
    language_switch,
    words_mismatch
)

# Check if we have a cached backend
if cache_key not in _PHONEMIZER_CACHE:
    # initialize the phonemization backend
    if backend == 'espeak':
        _PHONEMIZER_CACHE[cache_key] = BACKENDS[backend](
            language,
            punctuation_marks=punctuation_marks,
            preserve_punctuation=preserve_punctuation,
            with_stress=with_stress,
            tie=tie,
            language_switch=language_switch,
            words_mismatch=words_mismatch,
            logger=logger)
    elif backend == 'espeak-mbrola':
        _PHONEMIZER_CACHE[cache_key] = BACKENDS[backend](
            language,
            logger=logger)
    else:  # festival or segments
        _PHONEMIZER_CACHE[cache_key] = BACKENDS[backend](
            language,
            punctuation_marks=punctuation_marks,
            preserve_punctuation=preserve_punctuation,
            logger=logger)

phonemizer = _PHONEMIZER_CACHE[cache_key]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions