ollama - ✅(Solved) Fix sqlite memory leak [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#15021Fetched 2026-04-08 01:21:55
View on GitHub
Comments
1
Participants
2
Timeline
11
Reactions
0
Author
Participants
Timeline (top)
referenced ×6cross-referenced ×3commented ×1labeled ×1

Fix Action

Fixed

PR fix notes

PR #15030: fix: close sqlite connection on app shutdown to prevent memory leak

Description (problem / solution / changelog)

When the Ollama desktop app shuts down, the sqlite database connection was never closed, causing WAL journal files and deleted data pages to not be reclaimed, leading to db.sqlite file size bloat on Windows (ollama/ollama#15021). This fix adds appStore.Close() to the shutdown sequence after the ollama server goroutine exits, ensuring WAL checkpoint runs and the connection is properly closed.

Changed files

  • app/cmd/app/app.go (modified, +4/-0)
RAW_BUFFERClick to expand / collapse

What is the issue?

  1. run ollama
  2. make one or many long chats with ai
  3. remove all chats
  4. close(quit) ollama
  5. go to %localappdata%\Ollama
  6. db.sqlite have big size

solution: made VACUUM and REINDEX sqlite on quit ollama

Relevant log output

OS

Windows

GPU

No response

CPU

No response

Ollama version

0.18.2

extent analysis

Fix Plan

To reduce the size of the db.sqlite file, we need to implement the VACUUM and REINDEX operations when Ollama quits. Here are the steps:

  • Modify the Ollama application to execute SQL commands when it closes:
    • VACUUM to reclaim unused space
    • REINDEX to rebuild indexes for optimal performance
  • Example code snippet in Python using sqlite3 library:
import sqlite3

# Connect to the SQLite database
conn = sqlite3.connect('%localappdata%/Ollama/db.sqlite')
cursor = conn.cursor()

# Execute VACUUM and REINDEX commands
cursor.execute('VACUUM')
cursor.execute('REINDEX')

# Close the connection
conn.close()
  • Call this code when Ollama quits, for example, using the atexit library in Python:
import atexit

def cleanup_db():
    # VACUUM and REINDEX code here

atexit.register(cleanup_db)

Verification

To verify that the fix worked, follow these steps:

  • Run Ollama and repeat the steps that caused the issue
  • Close Ollama and check the size of the db.sqlite file
  • The file size should be significantly reduced after the VACUUM and REINDEX operations

Extra Tips

  • Make sure to handle any potential errors that may occur during the VACUUM and REINDEX operations
  • Consider scheduling regular maintenance tasks to keep the database optimized and reduce the need for manual intervention.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING