Jersey Client, memory leak, static and concurrency

Dejell

I am using Jersey Client (v2.17) to make external calls from my app.

I found out this memory leak bug (the CPU consumption was very high) which made me re-write my code to make the client static like this:

public class GeneralUtil {

    private static final Client client = ClientBuilder.newClient()


    public static String makeCall(String url) throws NotFoundException {
        return client.target(url).request().get(String.class);
      }
}

However, now I have concurrency problems - I am using this class to be called from multiple threads. I keep on getting:

org.apache.http.impl.execchain.RequestAbortedException: Request aborted

Any suggestion - how can I still prevent the memory leak, and still use the client?

aioobe

If you don't want to create an arbitrary number of Client objects, you can use ThreadLocal and have one object per thread.

You can override ThreadLocal.initialValue to return ClientBuilder.newClient() to automate creation of Client objects for new threads.

Or you could make the methods synchronized, but that means that you will only be able to do one request at a time.

Here's some example code:

class GeneralUtil {

    ThreadLocal<Client> client = new ThreadLocal<Client>() {
        @Override
        public Client initialValue() {
            return ClientBuilder.newClient();
        }
    };

    public static String makeCall(String url) throws NotFoundException {
        return client.get().target(url).request().get(String.class);
    }

    ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jersey Client, memory leak, static and concurrency

From Dev

Android memory leak on static Resource member variable?

From Dev

SpriteKit Memory Leak on static Menu scene

From Dev

SpriteKit Memory Leak on static Menu scene

From Dev

Live555 client streaming memory leak

From Dev

Memory leak in Google PubSub Python client

From Dev

Memory leak in between Jersey @BeanParam and HK2 ServiceLocatorImpl

From Dev

Memory leak despite destructor call using static unique_ptr

From Dev

Will initialization of static vector using a function give memory leak?

From Dev

CXF Bus for a client within a web service - memory leak

From Dev

Will TCP Socket Server client connection fd cause memory leak?

From Dev

Is this a memory leak

From Dev

Is there a memory leak?

From Java

Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

From Dev

Why does Android leak memory due to static Drawable if it's callback is reset?

From Dev

I am stuck between either a memory leak or excessive static in Audio while capturing audio from microphone

From Dev

"Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

From Dev

In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

From Dev

I am stuck between either a memory leak or excessive static in Audio while capturing audio from microphone

From Dev

How to use a class with a context argument in a static context without causing a memory leak?

From Dev

Can valgrind pick up a memory leak in a static library that im using from a C executable in a separate directory?

From Dev

Memory Leak in Scrapy

From Dev

javascript interval memory leak

From Java

Scrapy hidden memory leak

From Dev

Apache memory leak

From Dev

javascript - is that a memory leak?

From Dev

UIAlertController memory leak

From Dev

Memory leak(?) Python 3.2

From Java

React setTimeout - memory leak

Related Related

  1. 1

    Jersey Client, memory leak, static and concurrency

  2. 2

    Android memory leak on static Resource member variable?

  3. 3

    SpriteKit Memory Leak on static Menu scene

  4. 4

    SpriteKit Memory Leak on static Menu scene

  5. 5

    Live555 client streaming memory leak

  6. 6

    Memory leak in Google PubSub Python client

  7. 7

    Memory leak in between Jersey @BeanParam and HK2 ServiceLocatorImpl

  8. 8

    Memory leak despite destructor call using static unique_ptr

  9. 9

    Will initialization of static vector using a function give memory leak?

  10. 10

    CXF Bus for a client within a web service - memory leak

  11. 11

    Will TCP Socket Server client connection fd cause memory leak?

  12. 12

    Is this a memory leak

  13. 13

    Is there a memory leak?

  14. 14

    Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

  15. 15

    Why does Android leak memory due to static Drawable if it's callback is reset?

  16. 16

    I am stuck between either a memory leak or excessive static in Audio while capturing audio from microphone

  17. 17

    "Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

  18. 18

    In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

  19. 19

    I am stuck between either a memory leak or excessive static in Audio while capturing audio from microphone

  20. 20

    How to use a class with a context argument in a static context without causing a memory leak?

  21. 21

    Can valgrind pick up a memory leak in a static library that im using from a C executable in a separate directory?

  22. 22

    Memory Leak in Scrapy

  23. 23

    javascript interval memory leak

  24. 24

    Scrapy hidden memory leak

  25. 25

    Apache memory leak

  26. 26

    javascript - is that a memory leak?

  27. 27

    UIAlertController memory leak

  28. 28

    Memory leak(?) Python 3.2

  29. 29

    React setTimeout - memory leak

HotTag

Archive