aws-sdk-java icon indicating copy to clipboard operation
aws-sdk-java copied to clipboard

major memory leak in 1.11.x

Open if6was9 opened this issue 10 years ago • 10 comments

1.11.* introduced a very significant memory leak in SSL connection management.

I don't have a lot of data to work with yet, but our heap was filling up with SSL-related data within HashMap nodes.

Rollback to 1.10.x provided immediate relief.

The classes in question were all HTTPS-related...SSLContext, X509TrustManager, etc. Looking through the source code, Http(s) connection management seems that this has been significantly refactored in 1.11.x.

Our application is long-lived and very active, primarily EC2 and CodeDeploy APIs. We do tend to create a lot of Clients over time, rather than re-use them. So it is possible that we are using the SDK in a way that it was not designed to be used.

Nevertheless, the change in behavior was very sharp. Within several hours, our application would accumulate several gigs worth of a wide variety of SSL-related classes in the OldGen portion of the heap and die sad death of GC Overhead.

I will try to put together some test code that demonstrates the behavior.

if6was9 avatar May 30 '16 03:05 if6was9

image

This heap dump screenshot may be somewhat helpful.

if6was9 avatar May 30 '16 03:05 if6was9

Just trolling through code, the following stands out:

https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/internal/SdkSSLContext.java

https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/http/apache/client/impl/ApacheConnectionManagerFactory.java#L84-L92

My guess here is that the multiplicity of SSLContext-to-Client has gone from a globally shared model to a 1:1 model whereas it may have been 1:n in 1.10.x.

if6was9 avatar May 30 '16 03:05 if6was9

Related code back in 1.10.x:

https://github.com/aws/aws-sdk-java/blob/4156f65328122bb6a1568c3df7746b5c7668c752/aws-java-sdk-core/src/main/java/com/amazonaws/http/HttpClientFactory.java

if6was9 avatar May 30 '16 03:05 if6was9

In particular, it appears here that if a SocketFactory isn't globally set that one will be created repeatedly.

https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/http/apache/client/impl/ApacheConnectionManagerFactory.java#L84-L92

This does strike me as a defect. Once an SSLContext has been initialized for use by the SDK, it really should be re-used.

This is entirely consistent with the behaviors we are seeing. Too many SSLContext instances.

if6was9 avatar May 30 '16 03:05 if6was9

Looking into this.

manikandanrs avatar May 31 '16 18:05 manikandanrs

We think we've tracked this down. Testing now to confirm.

The suspected problem lies with IdleConnectionManager. We create a proxy of apache's connection manager for metric purposes. In 1.10 we registered the original connection manager with the reaper but in 1.11 we introduced a regression that registered the proxy instance with the connection manager. This meant that connection managers could be registered with the reaper but not deregistered because proxy.equals(otherProxy) will be unwrapped to orig.equals(otherProxy) in the invocation handler. The fix is to simply register the original connection manager rather then the proxied connection manager with the reaper.

shorea avatar Jun 01 '16 17:06 shorea

Fix has been confirmed. I'll see if we can get this released today.

shorea avatar Jun 01 '16 18:06 shorea

Thank you @shorea!

if6was9 avatar Jun 02 '16 01:06 if6was9

Hey sorry I didn't get around to cutting a release today. We've got one scheduled for tomorrow and this will be included. If you need a temporary fix disabling the reaper via ClientConfiguration will prevent the leak.

shorea avatar Jun 02 '16 01:06 shorea

This was released in 1.11.6

shorea avatar Jun 06 '16 15:06 shorea