Skip to content

Commit

Permalink
Fix memory leak (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-on authored Oct 24, 2024
1 parent af3b609 commit 9f56759
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions src/utils/ragEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/vector-db/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class VectorDbManager {
return manager
}

async cleanup() {
await this.repository.cleanup()
}

async performSimilaritySearch(
queryVector: number[],
embeddingModel: EmbeddingModel,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/vector-db/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
Expand Down

0 comments on commit 9f56759

Please sign in to comment.