-
Notifications
You must be signed in to change notification settings - Fork 62
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
Increasing memory usage on writes #1618
Comments
➤ rorbech commented: This is public variant of HELP-53315 to investigate potential memory leak on writes ... though we cannot currently replicate it. |
This seems to replicate it on And Pixel 5 (API 33) emulator:
At least it shows an increase in the |
After more testing, it seems to be related to the number of active versions: Modifying the code to
Show that the number of active versions keep increasing even though nothing should hold on to them. My best guess is that that it is our internal GC that isn't fast enough to keep up with the massive number of references being created. |
Surprisingly, the above run actually eventually seemed to catch up: But then crashed because it went OOM:
|
After looking more into this, it looks like this behavior can be explained by our RealmFinalizer thread either not keeping up or pointers simply not being GC'ed in time. By adding an atomic counter to the finalizer, I was able to show that the reference queue keeps growing with the following code:
I could see incremental bursts of things being GC'ed, but the overall trend was that the queue kept growing and growing.... just pausing the writes didn't help either. My guess there is that the memory allocator didn't consider the many thousand NativePointers important enough to GC. The result of this would either be that 1) We ran out of Disk space because the Realm file kept growing (because of unclaimed versions) or 2) We went OOM because we exhausted the native memory space. Only by stopping the writes and then manually calling the GC was I able to empty the queue. This is not ideal in "fast write"-scenarios like listening to sensor updates. I tried to modify our GC thread to have Max priority. This seemed to help a little bit, but the queue of pointers was still growing. So right now I guess that for these scenarios, we need some sort of "allocation-free" insert, or at least an insert that automatically cleans up as soon as the write is completed. In Realm Java we have a bulk insert method called |
A solution to this problem is most likely something like: #959 |
Any chance fixing this issue will be prioritized soon? @cmelchior , Did you figure out any workaround in the meantime? |
Hi, we moved to Realm Kotlin from Realm Java in our project and we see 10-30% increase in memory usage.
This usecase shows that native memory size doesn't decrease after closing Realm and triggering GC. Here is a test project I used for profiling: https://github.com/santaevpavel/realm-benchmark Realm version: 2.3.0 and 3.0.0 |
I also tried to figure out what makes memory usage high when
So iterating I wasn't able to debug it as native calls can't be debugged without sources. Also I tried to run android tests in this repository, but it didn't work. So basically it's just my thoughts. @nhachicha could you check it or confirm that the problem that I described is correct? |
I've found what's is leaking. It's happening in JNI layer that is generated by SWIG. Array of longs are not released. I've already found a fix. I will open a PR. |
It looks like we might have a memory leak somewhere involving writes. A simple loop like this running on Android shows a small continuous increase in memory usage
It can take several minutes (if not more for it to show, so I am still not 100% sure there is a leak, but we have a customer is running into crashes after several hours with a loop that writes 25 times pr. second)
The text was updated successfully, but these errors were encountered: