openai / openai/tiktoken

Unable to Release Memory Used by Tiktoken Encoding After Setting to None and GC Collect

Open
#414 0 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
19.3k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

Description

I'm experiencing an issue where memory allocated by loading a Tiktoken encoding (e.g., for "gpt-4o") is not released even after setting the tokenizer reference to None and calling gc.collect(). This leads to persistent high memory usage, which is problematic for long-running applications or environments with limited resources.

Steps to Reproduce
  1. Install tiktoken version 0.9.0.
  2. Run the following Python script:
import psutil
import gc
process = psutil.Process()

def get_memory():
    print(process.memory_info().rss / 1000000)

get_memory()  # Baseline memory

import tiktoken
get_memory()  # After importing tiktoken

tokenizer = tiktoken.encoding_for_model("gpt-4o").encode
get_memory()  # After loading the encoding

tokenizer = None
gc.collect()
get_memory()  # After releasing reference and GC
Expected Behavior

After setting tokenizer = None and calling gc.collect(), the memory usage should decrease back to near the level after importing tiktoken (around 18 MB in my test), as the encoding object is no longer referenced.

Actual Behavior

Memory remains at the elevated level (around 117 MB) even after releasing the reference and garbage collection. Output from my run:

13.729792
18.284544
117.604352
117.604352
Environment
  • Python version: 3.11
  • Tiktoken version: 0.9.0
  • OS: macOS 15.3.1
Additional Context

Is there a recommended way to explicitly release the memory used by the tiktoken encoding? For example, does tiktoken cache encodings internally, or is there a method to unload them? Any workarounds or fixes would be appreciated, as this impacts memory management in production scenarios.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied Python reproduction with tiktoken 0.9.0 and compare memory before and after releasing the encoding reference. Trace the encoding_for_model entry point and the encoding lifecycle to determine whether an internal cache retains it; done should be a verified release path or a documented explanation and workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.