Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak #37

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
return repo
}

async cleanup() {
this.pgClient?.close()
this.db = null
this.pgClient = null
}

async getIndexedFilePaths(embeddingModel: {
name: string
}): Promise<string[]> {
Expand Down Expand Up @@ -163,7 +169,7 @@
const similaritySearchResult = await this.db
.select({
...(() => {
const { embedding, ...rest } = getTableColumns(vectors)

Check warning on line 172 in src/utils/vector-db/repository.ts

View workflow job for this annotation

GitHub Actions / check

'embedding' is assigned a value but never used
return rest
})(),
similarity,
Expand Down
Loading