Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

How can I destroy the database and in-memory lokidb instance on web #1303

Open KennanChan opened 2 years ago

KennanChan commented 2 years ago

I am using watermelondb with lokidb on web. I create database shardings to minimize the data transfered and stored in a single pull phase. When I change from a database sharding "A" to a new database sharding "B", I am wondering how I can make sure to destroy the sharding "A" and its in-memory lokidb to avoid memory leak.

KennanChan commented 2 years ago

I found an undocumented call:

database.adapter.underlyingAdapter?._driver?.loki?.close?.()

Is it safe to use? why not expose a close method?

KennanChan commented 2 years ago

the loki.close() just ensure the lokidb do some necessary operations like saveing dirty records etc, which does not destroy the database. I do not find any useful method that lokidb exposed to destroy in-memory data it holds.

It seems I am left to rely on the main gc process to collect the unused lokidb.

Anyone has ideas?

KennanChan commented 2 years ago

One thing to clarify is that what I mean "destroy" is not to destroy the database on indexedDB, but to destroy the database connnection and the in-memory database to free some memory, since my project is a mobile web page which will be running on some old devices