java - Jersey 2.x leaking memory (Finalizer) with simple POST call (Multithreaded) -
the following simple jersey call leaking objects finalizer queue:
public boolean startexperiment(experiment experiment) { final client client = clientbuilder.newclient(); response response = null; rundescriptor rundescr = experiment.getrundescriptor(); try { final webtarget webtarget = client.target(experimentcontroller.getinstance().getrunnerurl()).path("runs").path("startnewasyncrun"); response = webtarget.request().post(entity.entity(rundescr, mediatype.application_json)); if (response != null && response.getstatusinfo().getstatuscode() != status.created.getstatuscode()) { system.out.println("error, run not created! response info: " + response.getstatusinfo()); return false; } else { return true; } } { if(response != null) { response.close(); } client.close(); } }
jersey versions tested: 2.18, 2.20, 2.22.2, 2.23.1. code run multiple threads (each thread running once every minute, example). closing response , client should not necessary, added jsut in case test if help. eclipse mat shows finalizer queue growing objects of type:
- sun.security.ssl.sslsocketimplementation
- java.io.fileinputstream
- sun.net.www.protocol.https.delegatehttpsurlconnection
- org.glassfish.jersey.client.clientruntime
- zipfileinflaterinputstream
any or thoughts on matter appreciated!
while not familiar eclipse mat , showing you, proper behavior objects go through finalizer queue.
under normal circumstances, object overrides finalize
method queued finalization (which consists of calling finalize
method). general idea when garbage collector identifies object unreachable, looks see if finalize
method overridden , has not yet been called. if finalize
defined , has not yet been called, object queued finalization, otherwise memory collected.
the finalizer appears implemented queue serviced single thread. thread processes each object in queue calling finalize
. after call finalize
completes, object removed queue.
subsequently, when garbage collector again determines object unreachable, find finalize
overridden, has been called. member collected.
note using feature delays collection of garbage. , while convenient way clean object, jvm not guarantee when or if finalize
method called.
one related risk "finalizer starvation." mention "growing" perhaps issue facing. if create enough garbage requires finalization , if finalization takes long, can run out of memory because single finalizer queue cannot keep up. there number of ways deal situation:
- make execution of
finalize
complete (infinite loops bad) , quickly. - avoid synchronization in
finalize
. - make finalizer thread have highest execution priority. can accomplished creating garbage instance changes thread priority in finalizer - create 1 of these during app initialization.
if seeing starvation, use thread dump see finalizer thread working on.
Comments
Post a Comment