eclipse-ee4j / eclipse-ee4j/jersey

Performance issue caused by the WeakReference in AbstractJaxbProvider

Open
#5,843 6 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

https://github.com/eclipse-ee4j/jersey/blob/d9658aa3064236abf1280f16e1705454e5d0b599/media/jaxb/src/main/java/org/glassfish/jersey/jaxb/internal/AbstractJaxbProvider.java#L284

Hi everyone!

Since we're using a weak reference here instead of a strong reference, once a GC happens, it will cause the JAXBContext to be reinitialized, which is a pretty expensive operation.

In our service, by using a custom JaxbContextResolver, we managed to reduce the response time by about 100ms (P90). So, should we provide an option here to let users decide whether to use strong or weak references?(Or an LRU cache with a size limit) I understand that the purpose of using weak references might be to reduce memory usage, but in reality, many services are more sensitive to response time.

```java
@Provider
public class JaxbContextResolver implements ContextResolver {

private final Map, JAXBContext> contexts = new HashMap<>();
private final Lock lock = new ReentrantLock();

@Override
public JAXBContext getContext(Class type) {
JAXBContext context;
lock.lock();
try {
context = contexts.get(type);
if (context == null) {
context = JAXBContext.newInstance(type);
contexts.put(type, context);
}
} catch (JAXBException ex) {
throw new RuntimeException(ex);
} finally {
lock.unlock();
}
return context;
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.