capacitor-community / sqlite

Community plugin for native & electron SQLite databases
MIT License
426 stars 104 forks source link

Memory leak using .saveToStore() #545

Closed Sadi-1992 closed 1 week ago

Sadi-1992 commented 3 weeks ago

Describe the bug I have an Issue using "@capacitor-community/sqlite": "^5.7.2" with "typeorm": "^0.3.20", and "jeep-sqlite": "^2.7.0".

jeep-sqlite is needed to be able to persist the inMemory DB from @capacitor-community/sqlite (WebStore) into the LocalForage as indexedDB.

My Issue is that the space is increasing by the ammount of the whole data each time i run "saveToStore()"

if (this.sqliteParams.platform === 'web') await this.sqliteParams.connection.saveToStore(sqliteDataSource.dbName);

It seems like the DB inside indexedDB does not get updated, it is adding the whole data into it again and again instead.

Is this a known issue? Or is there something I should know or possibly doing wrong?

To Reproduce Steps to reproduce the behavior:

  1. InitWebStore
  2. run saveToStore
  3. Each time saveToStore is called the used Space is growing even if there are no changes on the DB (use Chrome or Edge Developer Tools to inspect the Space-Usage)

Expected behavior The used Space of the indexedDB should not grow by the whole inMemory DB size, it should only grow by the size of the new added data.

Desktop:

xie392 commented 3 weeks ago

I also encountered the same problem.And why the data I saved cannot be accessed after refreshing? It should be because there is no persistent storage in it.My storage method is as follows: image

jepiqueau commented 3 weeks ago

@xie392 i do not think it is the same issue. This is a refresh problem with typeOrm when you use jeep-sqlite you must set the autosave property to false meaning that you must save the database to store yourself. the problen when you do a refresh the database has not been save to store. for this if the platform is web you have to create a refreshListener which close the connection meaning that the database is save to store in the close connection process. something like

const refreshListener = window.addEventListener('beforeunload', async () => {
  here you close the connection
}

do not forget to remove the refresh listener when you leave the page

            window.removeEventListener('beforeunload', refreshListener);
jepiqueau commented 3 weeks ago

@Sadi-1992 can you share your app

xie392 commented 3 weeks ago

@xie392我不认为这是同一个问题。这是 typeOrm 的刷新问题,当您使用 jeep-sqlite 时,您必须将 autosave 属性设置为 false,这意味着您必须保存数据库来存储自己。刷新数据库时出现的问题尚未保存到存储中。为此,如果平台是网络,则必须创建一个刷新监听器来关闭连接,这意味着数据库将保存在关闭连接过程中。就像是

const refreshListener = window.addEventListener('beforeunload', async () => {
  here you close the connection
}

离开页面时不要忘记删除刷新侦听器

            window.removeEventListener('beforeunload', refreshListener);

address.I don't know if what I'm doing is correct.can you take a look at it for me.

jepiqueau commented 3 weeks ago

@Sadi-1992 use Chrome or Edge Developer Tools to inspect the Space-Usage. how do you inspect the Space-Usage ? Please tell me how to proceed

jepiqueau commented 3 weeks ago

@Sadi-1992 What is the size of your database?

Sadi-1992 commented 3 weeks ago

@jepiqueau Hi Mate, thanks for the reply! 👍

@Sadi-1992 use Chrome or Edge Developer Tools to inspect the Space-Usage. how do you inspect the Space-Usage ? Please tell me how to proceed

You can open developer tools in chrome with "ctrl + shift + i". Then go to "Application" and select "Storage" on the left side. There you can see the used storage of the whole application.

@Sadi-1992 What is the size of your database?

Its not necessary how big the database is to trigger the issue. In my case i only create four tables and fill in a first row inside one of them. After that, the usage of the storage is growing each time i trigger "saveToStore" even if i don't add any new data.

@Sadi-1992 can you share your app

unfotunately i can't share the app since its from my company. I could try to create an example if really needed.

jepiqueau commented 3 weeks ago

@Sadi-1992 i so this it sometimes increasing or decreasing when you refresh. I control at the jeepSqliteStore the size of the databases created under databases store object and here the the behaviour is correct.

jepiqueau commented 3 weeks ago

@Sadi-1992 if you close the browser and restart the app the size of the store decreasing to the normal size. It is probably a refresh of the indexedDB which is not correctly done in that command giving the graph of the storage

jepiqueau commented 3 weeks ago

@Sadi-1992 i update jeep-sqlite by adding a Test Memory Leak in the Tests Menu. Before starting the test i delete jeepSqliteStore in the localhost:3333 and i run the component in the Home page few databases (9) are created if you look at the Chrome Development Tools Storage you get 2.4MB shows in the graph. if you go inside the store databases in jeepSqliteStore you will see at the item 3 the key dbForCopySQLite.db with a value having a a byte length of 28672 now run the Test Memory Leak it is going to insert 2 times a set of 1000 names in the areas table you will see this in the console 2023-04-24-Log_Test_Memory_Leak.log if you look at the storage in the Chrome Development Tools you will see 2.5MB and if you look at the application and refresh the jeepSqliteStore you will see that the dbForCopySQLite.db as a length of 315392 which is correct. Now if you refresh Test Memory Leak page you will see the storage value increasing to 2.6MB. So for me there is no issue

jepiqueau commented 3 weeks ago

@Sadi-1992 you must use jeep-sqlite@2.7.1 for this above

jepiqueau commented 3 weeks ago

@xie392 i fork it but i cannot run pnpm i i got this type of message ``` WARN  GET https://registry.npmjs.org/@testing-library%2Fdom error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.  WARN  GET https://registry.npmjs.org/@testing-library%2Fjest-dom error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.  WARN  GET https://registry.npmjs.org/@testing-library%2Freact error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.  WARN  GET https://registry.npmjs.org/@testing-library%2Fuser-event error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.

xie392 commented 3 weeks ago

@xie392 i fork it but i cannot run pnpm i i got this type of message ``` WARN  GET https://registry.npmjs.org/@testing-library%2Fdom error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.  WARN  GET https://registry.npmjs.org/@testing-library%2Fjest-dom error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.  WARN  GET https://registry.npmjs.org/@testing-library%2Freact error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.  WARN  GET https://registry.npmjs.org/@testing-library%2Fuser-event error (ERR_INVALID_THIS). Will retry in 10 seconds. 2 retries left.

I have an instance running right now, I'm not sure when I'm going to shut it down, if you have some time now you can take a look. address .This is my workspace

jepiqueau commented 3 weeks ago

@xie392 i got it working by going down to capacitor5 but what is your question? i got the home page when i go to user i cannot add a user or get the list of users

xie392 commented 3 weeks ago

@xie392 i got it working by going down to capacitor5 but what is your question? i got the home page when i go to user i cannot add a user or get the list of users After refreshing, search for the data just added, but cannot find it.

xie392 commented 3 weeks ago

@xie392 i got it working by going down to capacitor5 but what is your question? i got the home page when i go to user i cannot add a user or get the list of users After refreshing, search for the data just added, but cannot find it.

image image Also, in a database that has no tables at the beginning, do I have to enable this every time? Is this correct? image

jepiqueau commented 2 weeks ago

@Sadi-1992 Have you time to have a look and test?

jepiqueau commented 1 week ago

@Sadi-1992 As i do not get feedback and i demonstrate that there is no issue i close it