docling-project / docling-project/docling

GPU Memory leak when deleting DocumentConverter instance (~170MB retained)

Open
#2,954 7 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
66.4k
Forks
4.8k
Avg merge
2d 21h
Merged PRs (30d)
84

Description

### Bug
I am encountering a GPU memory leak when using `DocumentConverter` with `PdfPipelineOptions`.

Even after explicitly clearing the `initialized_pipelines`, deleting the converter instance, and performing garbage collection, approximately **172 MB** of GPU memory remains allocated. This suggests that the underlying models (likely related to OCR or the TableFormer) are not being fully detached or released from the CUDA device upon object destruction.

### Steps to reproduce
1. Initialize `DocumentConverter` with `PdfPipelineOptions` and `EasyOcrOptions`.
2. Convert a single PDF file.
3. Clear pipelines and delete the converter instance.
4. Check `torch.cuda.memory_allocated()`.

#### **Code to Reproduce**

```python
import torch
import gc
import time
from pathlib import Path
from docling.datamodel.pipeline_options import (
EasyOcrOptions,
PdfPipelineOptions,
)
from docling.document_converter import (
DocumentConverter as DocumentConverterDocling,
PdfFormatOption
)
from docling.datamodel.base_models import InputFormat

FILE_PATH = "my_file.pdf"

def convert_instance():
pipeline_options = PdfPipelineOptions(
enable_remote_services=True,
artifacts_path="/path/to/models",
do_table_structure=True,
do_picture_description=False,
force_ocr=False,
do_ocr=True,
do_picture_classification=False,
do_code_enrichment=False,
)
pipeline_options.ocr_options = EasyOcrOptions(
model_storage_directory="/path/to/easyocr"
lang=["en"],
)
converter = DocumentConverterDocling(
format_options={
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
}
)
return converter

# --- Execution & Measurement ---

# 1. Baseline
torch.cuda.empty_cache()
gc.collect()
baseline_mem = torch.cuda.memory_allocated() / 1024**2
print(f"Baseline memory: {baseline_mem:.2f} MB")

# 2. Conversion
converter = convert_instance()
converter.convert(FILE_PATH)
completion_mem = torch.cuda.memory_allocated() / 1024**2
print(f"After completion memory: {completion_mem:.2f} MB")

# 3. Cleanup attempt
converter.initialized_pipelines.clear()
del converter
gc.collect()
torch.cuda.empty_cache()

# 4. Final check
final_mem = torch.cuda.memory_allocated() / 1024**2
print(f"Final memory: {final_mem:.2f} MB")

```

#### **Logs / Output**

```text
Baseline memory: 0.0 MB
After completion memory: 471.97 MB
Final memory: 172.41 MB
```

### Docling version
docling==2.72.0

### Python version
python 3.12.12

### Additional Context

**Note on the environment:**
I am initializing the `DocumentConverter` with explicit local paths (`artifacts_path` and `model_storage_directory`) because I am running this in an **offline (air-gapped) environment** without internet access.

While I understand this is a custom configuration, loading models from a local path rather than downloading them should not affect the object's lifecycle. The memory cleanup and garbage collection should work seamlessly regardless of the model source.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.