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

Memory leak when creating many HttpClients #52541

Closed
blaugold opened this issue May 28, 2023 · 8 comments
Closed

Memory leak when creating many HttpClients #52541

blaugold opened this issue May 28, 2023 · 8 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report library-io

Comments

@blaugold
Copy link
Contributor

blaugold commented May 28, 2023

Observed on:

Dart SDK version: 2.19.6 (stable) (Tue Mar 28 13:41:04 2023 +0000) on "macos_arm64"
Dart SDK version: 3.0.2 (stable) (Tue May 23 08:26:58 2023 +0000) on "macos_arm64

The following program has a memory leak:

import 'dart:io';

void main() async {
  final pendingWork = <Future<void>>[];
  while (true) {
    if (pendingWork.length >= Platform.numberOfProcessors) {
      await Future.any(pendingWork);
    } else {
      final work = asyncWork();
      pendingWork.add(work);
      work.then((_) => pendingWork.remove(work));
    }
  }
}

Future<void> asyncWork() async {
  final client = HttpClient();
  try {
    final request =
        await client.openUrl('GET', Uri.parse('https://google.com'));
    final response = await request.close();
    await response.drain();
  } finally {
    client.close();
  }
}

Why do I create a new HttpClient for each request? I noticed the memory leak when using Isolate.run to parallelize work, where each Isolate needs its own HttpClient. But the multiple Isolates don't seem to be the problem, as the code above shows.

In the actual app I'm using package:http, which has the same issue, but I tried to make the repro as minimal as possible.

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io labels May 28, 2023
@a-siva
Copy link
Contributor

a-siva commented Jun 6, 2023

@blaugold are you running your app connected under devtools when you observe the memory leak? Wanted to confirm if what you are seeing is a duplicate of flutter/devtools#3290

@blaugold
Copy link
Contributor Author

blaugold commented Jun 7, 2023

@a-siva I noticed the issue in production where we use an AOT build and replicated it running the repro locally with JIT but never using devtools, so I don't think its a duplicate.

@a-siva
Copy link
Contributor

a-siva commented Jun 20, 2023

@blaugold could you give us some information on what tools you are using to observe the memory leak. I tried your test application above by running it for a long time and I did not observe any leaks while monitoring the Dart heap size or the native RSS.

[ GC isolate   | space (reason)           | GC# | start | time | used (MB)   | capacity MB | external| used (MB)     | capacity (MB) | external MB |  buffer | new  | old   ]
[              |                          |     |  (s)  | (ms) |before| after|before| after| b4 |aftr| before| after | before| after |before| after| b4 |aftr| (MB) | (MB)  ]
[ main         ,    Scavenge(   new space), 27201, 9836.36,   5.1,  32.0,   1.2,  32.0,  32.0, 15.8, 1.5,   10.3,   10.3,   30.8,   30.8,   0.7,   0.7,   1,   1, -30.8,    0.0, ]
[ main         ,    Scavenge(   new space), 27202, 9837.03,   5.3,  32.0,   1.3,  32.0,  32.0, 16.0, 1.6,   10.3,   10.3,   30.8,   30.8,   0.7,   0.7,   1,   1, -30.7,    0.0, ]
[ main         ,    Scavenge(   new space), 27203, 9837.73,   5.5,  32.0,   1.3,  32.0,  32.0, 15.8, 1.6,   10.3,   10.3,   30.8,   30.8,   0.7,   0.7,   1,   1, -30.7,    0.0, ]

image

@blaugold
Copy link
Contributor Author

That's interesting. We first noticed the memory issue in our Cloud Run deployment, which started to run out of memory after a certain amount of time, after we started creating many more HttpClients than before. Then I tried to replicate the issue locally on my MacBook using Activity Monitor:

Screenshot 2023-06-20 at 19 30 43

The highlighted process starts at around ~50 MB and slowly accumulates more memory use.

@blaugold
Copy link
Contributor Author

Here are a few more infos for the process:

Screenshot 2023-06-20 at 19 47 13

@blaugold
Copy link
Contributor Author

blaugold commented Aug 9, 2023

Seems like this is the same issue as #53113. When I replace https://google.com with http://google.com the code above does not exhibit the described leak.

@a-siva
Copy link
Contributor

a-siva commented Aug 9, 2023

Seems like this is the same issue as #53113. When I replace https://google.com with http://google.com the code above does not exhibit the described leak.

Thanks for that clarification, I will dup this issue into that one.

@a-siva a-siva added the closed-duplicate Closed in favor of an existing report label Aug 9, 2023
@a-siva
Copy link
Contributor

a-siva commented Aug 9, 2023

Marking as a duplicate of #53113 and closing.

@a-siva a-siva closed this as completed Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report library-io
Projects
None yet
Development

No branches or pull requests

3 participants