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

Out Of Memory crashes since upgrading to v. 10.8.0 #4016

Closed
martinpoulsen opened this issue Oct 11, 2021 · 7 comments · Fixed by #4024
Closed

Out Of Memory crashes since upgrading to v. 10.8.0 #4016

martinpoulsen opened this issue Oct 11, 2021 · 7 comments · Fixed by #4024
Assignees

Comments

@martinpoulsen
Copy link

martinpoulsen commented Oct 11, 2021

How frequently does the bug occur?

Sometimes

Description

We are seeing a lot of "Out Of Memory" crashes in production on both iOS and Android since we upgraded realm from 10.2.0 to 10.8.0.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

I have reproduced in this template app + realm 10.8.0:

https://github.com/martinpoulsen/realm-memory-issue

The app will populate "RoutePoint" entities to a "Trip" entity periodically initiated on app launch:

Schema:

const tripSchema = {
  name: 'Trip',
  primaryKey: 'id',
  properties: {
    id: 'string',
    updatedAt: {type: 'date', optional: true, default: new Date()},
    route: {
      type: 'list',
      objectType: 'RoutePoint',
    },
  },
};

const routePointSchema = {
  name: 'RoutePoint',
  primaryKey: 'id',
  properties: {
    id: 'string',
    lat: 'double',
    lon: 'double',
    time: {type: 'date', optional: true},
    alt: {type: 'int', optional: true},
    accuracy: {type: 'int', optional: true},
    speed: {type: 'int', optional: true},
    course: {type: 'int', optional: true},
    battery: {type: 'double', optional: true},
    invalid: {type: 'int', optional: true},
    deletedAt: {type: 'date', optional: true},
  },
};

const schema = [tripSchema, routePointSchema];
const schemaVersion = 1;
const migration = (oldRealm, newRealm) => {};

export const createRealmInstance = () => {
  const config = {
    schema,
    schemaVersion,
    path: 'realmfile',
    migration,
  };
  return new Realm(config);
};

Adding route points:

const trip = realm.objects('Trip').filtered(`id == "${tripId}"`)[0];
console.log('retrieved trip', trip.id, JSON.stringify(trip.route));

const newRoute = Array.from(trip.route);
const routePointId = getRoutePointUUID(i++);
newRoute.push({
  ...routePointStub,
  id: routePointId,
  time: new Date(),
});

realm.write(() => {
  realm.create(
    'Trip',
    {
      id: trip.id,
      updatedAt: new Date(),
      route: newRoute,
    },
    true,
  );
});After ~10 minutes (adding ~1300 route points), Android Studio memory profiling shows that the app uses ~800 MB of memory in the native layer and it only keeps growing from there.

realm-memory-problem-10UNITO-UNDERSCORE!8!UNITO-UNDERSCORE!0!

If the realm version in the sample app is downgraded to 10.2.0 and the app is deleted/reinstalled, then after 10 minutes the app only takes up less than 100 MB of memory in the native layer and this amount seems relatively stable.

realm-memory-problem-10UNITO-UNDERSCORE!2!UNITO-UNDERSCORE!0!

I have noticed that if I don't log JSON.stringify(trip.route) then the problem significantly decreases and if I don't log trip.id then the problem almost goes away. However, in our production app we don't log or stringify the trip or route, but we must be triggering the problem some other way.

We can't downgrade the realm version in our production app due to the internal realm file format version so a quick fix or ideas for a workaround would be much appreciated!

Version

10.8.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS 14, Android 11, etc.

Build environment

Which debugger for React Native: ..

@kneth
Copy link
Contributor

kneth commented Oct 11, 2021

@martinpoulsen Thank you for your report and your app for reproducing the issue.

Unrelated to your issue, I would change const trip = realm.objects('Trip').filtered(id == "${tripId}")[0]; to const trip = realm.objectForPrimaryKey('Trip', tripid);. It is more readable and slightly faster 😄

We need to investigate (by running your app) before we can say something conclusively.

Btw, we haven't changed the file format since 10.5.0, and you might want to try an earlier version.

@martinpoulsen
Copy link
Author

@kneth thanks for the quick reply and the suggested downgrade workaround. Unfortunately, I'm also seeing the issue in v. 10.5.0.

Btw, thanks for the objectForPrimaryKey tip 👍

@kneth
Copy link
Contributor

kneth commented Oct 13, 2021

@martinpoulsen By using your reproduction app we have been able to identify which commit is causing the issue. We are working on a fix in #4024

@kneth kneth mentioned this issue Oct 13, 2021
3 tasks
@martinpoulsen
Copy link
Author

@kneth Very glad to hear that! Thanks for moving so quickly on this 👍

@sync-by-unito sync-by-unito bot assigned fronck and unassigned kneth Oct 15, 2021
@cristianoccazinsp
Copy link

I'm using 10.8.0 as well, but haven't seen OOM issues. What is the specific use case that will trigger this so I can review our code to make sure we are not missing this until the patch is merged?

@fronck
Copy link

fronck commented Oct 20, 2021

@martinpoulsen Thanks a lot for providing the reproduction code.

@martinpoulsen @cristianoccazinsp We've released Realm-JS v10.9.1, which addresses a memory leak in the type handling code. I've profiled the fix with Martin's code, and it shows a large reduction in memory usage over time.

I'll close this issue. Please feel free to re-open if this does not address your issues.

@martinpoulsen
Copy link
Author

@kneth @fronck Awesome, thanks 🎉

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants