Investigate memory leaks when using subscriptions
A user sent me this:
Hi Nikola, we have most probably found the cause of the leaks. And now we need your help to find a way to fix it. First of all some details to better describe how we use REALM DB.
The app is a IoT SW that needs to be constantly up to date via Subscription to DB classes. This app is up and running 24x7x365. It’s never restarted or killed.
Now, we go deeper in our issue. When there is an update in one of the DB fileds, we see an increase of the app’s memory. Of course this behaviour can be seen after many hours. What have we done then to prove it:
We commented only the write operation leaving the app running for many hours – we saw almost no memory increase We made a demo app (GitHub - kalpasrl/RealmTest 2) to be run in the same conditions of our app We did a double test: Test 1 - With subscriptions and doing many write a minute - it’s a stress test in order to have results in a short time Test 2 - Without subscriptions doing the same test above Results: colors legenda: Brown line: native memory Orange line: private/managed memory Blue line: total memory
Test 1 – the memory usage is shown in the following picture
Test 2 – the memory usage is shown in the following picture
As we can see, it seems that as soon as I activate subscriptions and start updating the native memory starts leaking. This behaviour is not seen if I do the test with subscriptions deactivated.
The demo app is setup with subscriptions enabled. If you you want to run it without subscriptions, you have to comment the line 63 of App.xaml.cs file.
We should try to repro and understand what's causing this behavior.
Hi @papafe,
the graphs shown are produced with our internal tool that collects the results from the adb command:
adb shell dumpsys meminfo com.companyname.realmtest
Best regards Stefano
@kalpasrl Thanks for clarifying it!
@kalpasrl Can I ask you under which condition have you tested? In particular:
- Are you using a simulator or a real device?
- Are you using a debug or release build?
- Are you keeping the debugger attached while testing?
@papafe
- The graphics are produced with an Android emulator. With a real device, even if the values are different, the result does not change.
- The app was compiled in release mode.
- There is no debugger attached
@kalpasrl Thanks for the info. I was trying to reproduce the issue with the project you linked, but I managed only when using the app compiled in debug mode, and with the debugger attached.
I didn't manage to reproduce it in release mode, either with the debugger attached or not.
Interestingly enough, when I managed to reproduce it (debug mode, debugger attached) I had a similar graph even by just using Debug.WriteLine with a fixed string (for example Debug.WriteLine("something"), instead of printing the number of indices in changes
I am closing this since it has been opened for some time and we did not receive additional information. Feel free to reopen it if you still have this issue.
Test 2 – the memory usage is shown in the following picture
As we can see, it seems that as soon as I activate subscriptions and start updating the native memory starts leaking.
This behaviour is not seen if I do the test with subscriptions deactivated.