diff --git a/src/ChatView.tsx b/src/ChatView.tsx index 66364bb..c6cc9d3 100644 --- a/src/ChatView.tsx +++ b/src/ChatView.tsx @@ -57,7 +57,16 @@ export class ChatView extends ItemView { this.root = createRoot(this.containerEl.children[1]) } - const queryClient = new QueryClient() + const queryClient = new QueryClient({ + defaultOptions: { + queries: { + gcTime: 0, // Immediately garbage collect queries. It prevents memory leak on ChatView close. + }, + mutations: { + gcTime: 0, // Immediately garbage collect mutations. It prevents memory leak on ChatView close. + }, + }, + }) const ragEngine = await this.plugin.getRAGEngine() this.root.render( diff --git a/src/main.ts b/src/main.ts index 02dbcc4..a9730b1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -111,7 +111,10 @@ export default class SmartCopilotPlugin extends Plugin { this.addSettingTab(new SmartCopilotSettingTab(this.app, this)) } - onunload() {} + onunload() { + this.ragEngine?.cleanup() + this.ragEngine = null + } async loadSettings() { this.settings = parseSmartCopilotSettings(await this.loadData()) diff --git a/src/utils/ragEngine.ts b/src/utils/ragEngine.ts index 9c43026..4992a48 100644 --- a/src/utils/ragEngine.ts +++ b/src/utils/ragEngine.ts @@ -31,6 +31,10 @@ export class RAGEngine { return ragEngine } + async cleanup() { + await this.vectorDbManager.cleanup() + } + setSettings(settings: SmartCopilotSettings) { this.settings = settings this.embeddingModel = getEmbeddingModel(settings.embeddingModel, { diff --git a/src/utils/vector-db/manager.ts b/src/utils/vector-db/manager.ts index 958ba38..dea5c35 100644 --- a/src/utils/vector-db/manager.ts +++ b/src/utils/vector-db/manager.ts @@ -25,6 +25,10 @@ export class VectorDbManager { return manager } + async cleanup() { + await this.repository.cleanup() + } + async performSimilaritySearch( queryVector: number[], embeddingModel: EmbeddingModel, diff --git a/src/utils/vector-db/repository.ts b/src/utils/vector-db/repository.ts index a67c461..b33ef54 100644 --- a/src/utils/vector-db/repository.ts +++ b/src/utils/vector-db/repository.ts @@ -41,6 +41,12 @@ export class VectorDbRepository { return repo } + async cleanup() { + this.pgClient?.close() + this.db = null + this.pgClient = null + } + async getIndexedFilePaths(embeddingModel: { name: string }): Promise {