-
Notifications
You must be signed in to change notification settings - Fork 391
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
Potential Memory Leak when opening and closing connections & lazy database closure #3449
Comments
Thanks for the report! I've tried to reproduce it, but unfortunately wasn't successful. I've used this program: void main() async {
FlutterError.demangleStackTrace = (StackTrace stack) {
if (stack is Trace) return stack.vmTrace;
if (stack is Chain) return stack.toTrace().vmTrace;
return stack;
};
for (var i = 0; i < 10; i++) {
final db = AppDatabase(driftDatabase(
name: 'repro.db',
native: DriftNativeOptions(
shareAcrossIsolates: false,
databasePath: () async => '/tmp/test.db',
),
));
await db.customStatement('SELECT 1;');
await db.close();
}
print('at end');
} Setting a breakpoint on the print line at the end, opening devtools and starting one manual GC only shows a single |
Thanks for the fast response! I appreciate your time in trying to reproduce the issue. With your example, I also couldn't reproduce the issue. However, I forgot to mention an important detail...I am using Drift in combination with workmanager (v.0.5.2), where the database is opened and closed within a background task. It’s possible that the issue only occurs in this specific setup. Could you try testing it in a workmanager background task to see if the behavior changes? |
Good point, thanks! I've fixed that in 49ebf88. I'm not convinced that issue is what you were seeing with |
Oh well, as you suggested, I forgot to close the database, so it remained open and unused. After properly closing the database in the WorkManager task, the memory leak is now fixed. Thanks for pointing it out! |
We also had an aggregation of ![]() @devroble As I can see in your debugger-screenshot you also had an aggregation of these instances. Do you still have the same problem in your case? |
I couldn't reproduce that problem in a small demo that listens to stream queries and then closes an isolate database repeatedly. Are you listening to table updates manually or is this just through stream queries? |
Good observation, @johannessachse ! Yes, the aggregation of instances still exists, so there does seem to be an issue. While closing the database in the WorkManager task fixed one part of the problem, the accumulation of I assume, that this then()-part in
Retaining path for NotifyTableUpdated:
Retaining path for TableUpdate:
|
Hi, I work with Johannes and since he is on a well deserved vacation, I will try to pick this up. @devroble We are not using the The retain paths seem to indicate that this is being retained on the (drift) server side (drift isolate)., right? @simolus3 Not sure how to reproduce this in a small sample. |
Should we create another issue or reopen this issue? |
A potential memory leak occurs when repeatedly opening and closing connections in Drift. Instances of DriftCommunication accumulate over time and are not properly deallocated, leading to increasing memory usage. This issue suggests that resources tied to DriftCommunication are not being released when a connection is closed. NotifyTablesUpdated and TableUpdate are also affected.
Closing a lazy database without performing any operations beforehand results in an increasing number of isolates, suggesting a resource leak as well.
Steps to Reproduce:
Using:
The text was updated successfully, but these errors were encountered: