You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by kongar <ak...@5amsolutions.com> on 2012/07/21 02:49:48 UTC
JAXBContext caching and WeakReferences
Hi,
I am currently trying to tune the performance of my CXF Client, and I
noticed that during Service creation, the JAXBContext keeps getting created
even if I'm hitting the same endpoint just with different clients. I looked
into the CXF source, and I see that there is a caching mechanism that's
supposed to keep the context (org.apache.cxf.common.jaxb.JAXBContextCache),
but for one reason or another, the cache lookup almost always fails. It
seems like since the cache uses WeakReferences to hold the context, the
context gets garbage collected almost immediately and has to be recreated
each time. My calls to the same endpoint happen only a few seconds apart so
I'm a little surprised that it gets collected that quickly.
When I modified the JAXBContextCache (actually it's inner class
CachedContextAndSchemas) to use direct references instead of WeakReference,
this problem went away. The caching worked as expected and service creation
went much faster. But I would really prefer not to modify the cxf source,
so I'm wondering if there's something that I'm missing or something that I
can do so I can avoid rolling my own cxf build? I've looked into just
keeping a reference of the JAXBContext in my code to prevent it from being
garbage collected, but it's pretty deep into the cxf code and it doesn't
seem like there was a clean way of doing it.
--
View this message in context: http://cxf.547215.n5.nabble.com/JAXBContext-caching-and-WeakReferences-tp5711383.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: JAXBContext caching and WeakReferences
Posted by kongar <ak...@5amsolutions.com>.
Thanks for the reply! I went with #2 on your suggestion, and it seems to
work great. Have not seen the contexts getting recreated after the initial
hit.
--
View this message in context: http://cxf.547215.n5.nabble.com/JAXBContext-caching-and-WeakReferences-tp5711383p5711469.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: JAXBContext caching and WeakReferences
Posted by Henri Tremblay <he...@gmail.com>.
Hi,
The question might be silly but I don't really know what's in the
JAXBContext.
What is that bad in having a strong reference to it? I had the same issue
as kongar and my guess is that the cache is pretty much useless most of the
time because of the weak reference.
I will propose two solutions:
1- Use a SoftReference instead. It should be GCed but they tend to stick a
bit more around
2- Use a strong reference, and clear the cache in a ContextListener or so
WDYT?
Thanks,
Henri
On 23 July 2012 17:54, Daniel Kulp <dk...@apache.org> wrote:
>
> Chaning to a strong reference would definitely NOT be good as things would
> never get garbage collected, jars would remain locked, etc....
>
> Couple thoughts for you:
>
> 1) If you can get a list of all the classes CXF detects and asks for the
> JAXBContext for (may just be the ObjectFactories), you could ask the
> JAXBContext for the same context and hold onto one stongly.
>
> 2) You could just create CXF client proxy or similar that would use the
> same
> context and hold onto that strongly. You don't need to use it, just hold
> onto it so the context remains.
>
> Dan
>
>
>
> On Friday, July 20, 2012 05:49:48 PM kongar wrote:
> > Hi,
> >
> > I am currently trying to tune the performance of my CXF Client, and I
> > noticed that during Service creation, the JAXBContext keeps getting
> > created even if I'm hitting the same endpoint just with different
> > clients. I looked into the CXF source, and I see that there is a caching
> > mechanism that's supposed to keep the context
> > (org.apache.cxf.common.jaxb.JAXBContextCache), but for one reason or
> > another, the cache lookup almost always fails. It seems like since the
> > cache uses WeakReferences to hold the context, the context gets garbage
> > collected almost immediately and has to be recreated each time. My calls
> > to the same endpoint happen only a few seconds apart so I'm a little
> > surprised that it gets collected that quickly.
> >
> > When I modified the JAXBContextCache (actually it's inner class
> > CachedContextAndSchemas) to use direct references instead of
> > WeakReference, this problem went away. The caching worked as expected
> > and service creation went much faster. But I would really prefer not to
> > modify the cxf source, so I'm wondering if there's something that I'm
> > missing or something that I can do so I can avoid rolling my own cxf
> > build? I've looked into just keeping a reference of the JAXBContext in
> > my code to prevent it from being garbage collected, but it's pretty deep
> > into the cxf code and it doesn't seem like there was a clean way of doing
> > it.
>
> --
> Daniel Kulp
> dkulp@apache.org - http://dankulp.com/blog
> Talend Community Coder - http://coders.talend.com
>
Re: JAXBContext caching and WeakReferences
Posted by Daniel Kulp <dk...@apache.org>.
Chaning to a strong reference would definitely NOT be good as things would
never get garbage collected, jars would remain locked, etc....
Couple thoughts for you:
1) If you can get a list of all the classes CXF detects and asks for the
JAXBContext for (may just be the ObjectFactories), you could ask the
JAXBContext for the same context and hold onto one stongly.
2) You could just create CXF client proxy or similar that would use the same
context and hold onto that strongly. You don't need to use it, just hold
onto it so the context remains.
Dan
On Friday, July 20, 2012 05:49:48 PM kongar wrote:
> Hi,
>
> I am currently trying to tune the performance of my CXF Client, and I
> noticed that during Service creation, the JAXBContext keeps getting
> created even if I'm hitting the same endpoint just with different
> clients. I looked into the CXF source, and I see that there is a caching
> mechanism that's supposed to keep the context
> (org.apache.cxf.common.jaxb.JAXBContextCache), but for one reason or
> another, the cache lookup almost always fails. It seems like since the
> cache uses WeakReferences to hold the context, the context gets garbage
> collected almost immediately and has to be recreated each time. My calls
> to the same endpoint happen only a few seconds apart so I'm a little
> surprised that it gets collected that quickly.
>
> When I modified the JAXBContextCache (actually it's inner class
> CachedContextAndSchemas) to use direct references instead of
> WeakReference, this problem went away. The caching worked as expected
> and service creation went much faster. But I would really prefer not to
> modify the cxf source, so I'm wondering if there's something that I'm
> missing or something that I can do so I can avoid rolling my own cxf
> build? I've looked into just keeping a reference of the JAXBContext in
> my code to prevent it from being garbage collected, but it's pretty deep
> into the cxf code and it doesn't seem like there was a clean way of doing
> it.
--
Daniel Kulp
dkulp@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com