eclipse-ee4j / eclipse-ee4j/jersey
Performance issue caused by the WeakReference in AbstractJaxbProvider
- 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
Assessment
This issue has not been assessed yet.