question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak when creating many ImageAnnotatorClient instances

See original GitHub issue

Unsure what the root cause of the issue is but I noticed that creating a lot of ImageAnnotatorClient instances causes a consistent rise in unmanaged memory (first guess is - it’s creating a lot of HttpClients):

image

I was initialising a ImageAnnotatorClient per image and I am sending 50-100 images per second so I was quite quickly running out of memory. Now, I just reuse the same ImageAnnotatorClient for each batch of images and I now see fairly consistent memory usage (sorry for the differing pictures, the bottom graph shows the unmanaged memory - and it’s basically flatlined):

image

I was just wondering whether:

  1. It’s okay to reuse the client, several hundred times?
  2. Whether I’ve exposed a bug (maybe something isn’t being GC’d?)…

Is this possibly a duplicate/symptom of this issue or perhaps this issue

These are my installed packages:

  <package id="Google.Api.CommonProtos" version="1.2.0" targetFramework="net46" />
  <package id="Google.Api.Gax" version="2.2.1" targetFramework="net46" />
  <package id="Google.Api.Gax.Grpc" version="2.2.1" targetFramework="net46" />
  <package id="Google.Apis" version="1.32.2" targetFramework="net46" />
  <package id="Google.Apis.Auth" version="1.32.2" targetFramework="net46" />
  <package id="Google.Apis.Core" version="1.32.2" targetFramework="net46" />
  <package id="Google.Cloud.Vision.V1" version="1.1.0" targetFramework="net46" />
  <package id="Google.Protobuf" version="3.5.1" targetFramework="net46" />
  <package id="Grpc.Auth" version="1.10.0" targetFramework="net46" />
  <package id="Grpc.Core" version="1.10.0" targetFramework="net46" />

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
markwalsh-liverpoolcommented, Mar 7, 2018

Okay, great that makes perfect sense. In case anyone else is reading and finds they have the same problem I modified my code to be:

        private static ImageAnnotatorClient _client;
        public ImageAnnotatorClient Create()
        {
            if (_client == null)
            {
                var credential = GoogleCredential
                    .FromFile(AppDomain.CurrentDomain.BaseDirectory + "google-cloud-vision-license.json")
                    .CreateScoped(ImageAnnotatorClient.DefaultScopes);
                var channel = new Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(),
                    credential.ToChannelCredentials());
                _client = ImageAnnotatorClient.Create(channel);
            }

            return _client;
        }

Thanks Jon!

0reactions
evildourcommented, Mar 7, 2018

Note that if you’re using this from multiple threads, you may want to use Lazy<T> instead:

private static readonly Lazy<ImageAnnotatorClient> _client = new Lazy<ImageAnnotatorClient>(() => {
    var credential = GoogleCredential
        .FromFile(AppDomain.CurrentDomain.BaseDirectory + "google-cloud-vision-license.json")
        .CreateScoped(ImageAnnotatorClient.DefaultScopes);
    var channel = new Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(),
        credential.ToChannelCredentials());
    return ImageAnnotatorClient.Create(channel);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

What are the things I should avoid in my python Cloud ...
1 Answer. explicitly delete the temporary variables that you allocate on the bots processing path, which may be referencing each-other thus ...
Read more >
Investigating Memory Leaks in Google Cloud Datastore ...
A memory leak occurs when a program allocates memory but fails to release it when it is no longer needed. Over time, these...
Read more >
GAE - Nodejs - Memory leak for a simple pubsub app
You may not be experienceing a memory leak, but may just be constrained by the memory available. The default instance class of App...
Read more >
RAM isn't freed after a request in my Cloud Run service?
It certainly feels like a "memory leak". If we imagine a request being received, the request is processed by a Docker image that...
Read more >
How to Handle Google Cloud Pub/Sub Memory Leaks on ...
The memory leak issue on re-deploy occurs when the Netty-based client tries to close the connection to the Google Cloud Pub/Sub server. When...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found