Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in phonemize.py #198

Open
Dave1475 opened this issue Jan 20, 2025 · 0 comments
Open

Memory leak in phonemize.py #198

Dave1475 opened this issue Jan 20, 2025 · 0 comments

Comments

@Dave1475
Copy link

Dave1475 commented Jan 20, 2025

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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant