OpenLiberty / open-liberty

Open Liberty is a highly composable, fast to start, dynamic application server runtime environment
https://openliberty.io
Eclipse Public License 2.0
1.15k stars 592 forks source link

Warn when user apps may be "leaking" JAX-RS Client instances #7961

Open andymc12 opened 5 years ago

andymc12 commented 5 years ago

Per the Client javadocs: Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of a Client instance may be a rather expensive operation. It is therefore advised to construct only a small number of Client instances in the application. Client instances must be properly closed before being disposed to avoid leaking resources.

The best practice would be to create a small number of Client instances and re-use them to create multiple WebTarget instances (which could also be re-used in most cases). Then close the Client instance(s) when the app shuts down.

In cases where the application creates multiple Client instances and does not close them, they will leak resources in the com.ibm.ws.jaxrs20.client.JAXRSClientImpl class. If left unchecked, this could lead to out of memory errors.

The Liberty server should warn users when too many Client instances are in the JAXRSClientImpl map. I suggest using 1000 as the default threshold, but to make that number configurable - the reason for that is that this map is used for active clients, and it is possible that a normal application may be running with over 1000 active clients (or that some applications may be in memory constrained environments and may need the warning much earlier).

jhanders34 commented 5 years ago

For reference PR #7987 does now clean up JAXRSClientImpl resources on application stop so if a user does not close them on stop (probably usual), we do not leak any longer. The warning mentioned above though when a threshold is reached is still a good idea though.

andymc12 commented 2 years ago

RESTEasy does this - but it still might be nice to add for JAX-RS 2.0/2.1.