java.lang.OutOfMemoryError: Java heap space
I get Out of Memory Error after calling S3 putObject several times.
Describe the bug
java.lang.OutOfMemoryError: Java heap space
Expected Behavior
no Memory leak. keep running application without Error.
Current Behavior
Log from Profiler
<node call_tree="S3WriterImpl.java:74 software.amazon.awssdk.services.s3.DefaultS3Client.putObject(PutObjectRequest, RequestBody)" avg_objects_sec="155" recorded_objects="368877" size="35279944">
<node call_tree="DefaultS3Client.java:8297 software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler.execute(ClientExecutionParams)" avg_objects_sec="155" recorded_objects="368772" size="35277368">
<node call_tree="AwsSyncClientHandler.java:55 software.amazon.awssdk.core.client.handler.SdkSyncClientHandler.execute(ClientExecutionParams)" avg_objects_sec="155" recorded_objects="368767" size="35277248">
<node call_tree="SdkSyncClientHandler.java:45 software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.execute(ClientExecutionParams)" avg_objects_sec="155" recorded_objects="368767" size="35277248">
<node call_tree="BaseSyncClientHandler.java:94 software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.measureApiCallSuccess(ClientExecutionParams, Supplier)" avg_objects_sec="155" recorded_objects="368760" size="35277080">
<node call_tree="BaseSyncClientHandler.java:167 software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler$$Lambda$998.0x000000080080cb38.get()" avg_objects_sec="155" recorded_objects="368760" size="35277080">
<node call_tree="software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.lambda$execute$1(ClientExecutionParams)" avg_objects_sec="155" recorded_objects="368760" size="35277080">
<node call_tree="BaseSyncClientHandler.java:112 software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.doExecute(ClientExecutionParams, ExecutionContext, HttpResponseHandler)" avg_objects_sec="155" recorded_objects="367071" size="35197712">
<node call_tree="BaseSyncClientHandler.java:159 software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.invoke(SdkHttpFullRequest, SdkRequest, ExecutionContext, HttpResponseHandler)" avg_objects_sec="153" recorded_objects="362718" size="35037584">
<node call_tree="BaseSyncClientHandler.java:133 software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient$RequestExecutionBuilderImpl.execute(HttpResponseHandler)" avg_objects_sec="153" recorded_objects="362714" size="35037456">
<node call_tree="AmazonSyncHttpClient.java:193 software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362144" size="35025472">
<node call_tree="ExecutionFailureExceptionReportingStage.java:26 software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362144" size="35025472">
<node call_tree="ExecutionFailureExceptionReportingStage.java:37 software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362144" size="35025472">
<node call_tree="RequestPipelineBuilder.java:206 software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362138" size="35025328">
<node call_tree="RequestPipelineBuilder.java:206 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallMetricCollectionStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362138" size="35025328">
<node call_tree="ApiCallMetricCollectionStage.java:31 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallMetricCollectionStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362138" size="35025328">
<node call_tree="ApiCallMetricCollectionStage.java:48 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362133" size="35025208">
<node call_tree="ApiCallTimeoutTrackingStage.java:42 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362133" size="35025208">
<node call_tree="ApiCallTimeoutTrackingStage.java:60 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.executeWithTimer(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362133" size="35025208">
<node call_tree="ApiCallTimeoutTrackingStage.java:80 software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362122" size="35025032">
<node call_tree="StreamManagingStage.java:36 software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362122" size="35025032">
<node call_tree="StreamManagingStage.java:56 software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="362056" size="35022256">
<node call_tree="RequestPipelineBuilder.java:206 software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="360950" size="34985440">
<node call_tree="RetryableStage.java:34 software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="360950" size="34985440">
<node call_tree="RetryableStage.java:64 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptMetricCollectionStage.execute(Object, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="360357" size="34967032">
<node call_tree="ApiCallAttemptMetricCollectionStage.java:36 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptMetricCollectionStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="152" recorded_objects="360357" size="34967032">
<node call_tree="ApiCallAttemptMetricCollectionStage.java:50 software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(Object, RequestExecutionContext)" avg_objects_sec="151" recorded_objects="359762" size="34948448">
<node call_tree="TimeoutExceptionHandlingStage.java:39 software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="151" recorded_objects="359762" size="34948448">
<node call_tree="TimeoutExceptionHandlingStage.java:77 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(Object, RequestExecutionContext)" avg_objects_sec="151" recorded_objects="359762" size="34948448">
<node call_tree="ApiCallAttemptTimeoutTrackingStage.java:42 software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="151" recorded_objects="359762" size="34948448">
<node call_tree="ApiCallAttemptTimeoutTrackingStage.java:73 software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(Object, RequestExecutionContext)" avg_objects_sec="151" recorded_objects="359755" size="34948336">
<node call_tree="RequestPipelineBuilder.java:206 software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(Object, RequestExecutionContext)" avg_objects_sec="150" recorded_objects="356329" size="34847568">
<node call_tree="RequestPipelineBuilder.java:206 software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(Object, RequestExecutionContext)" avg_objects_sec="150" recorded_objects="356326" size="34847496">
<node call_tree="RequestPipelineBuilder.java:206 software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(Object, RequestExecutionContext)" avg_objects_sec="150" recorded_objects="355828" size="34830864">
<node call_tree="RequestPipelineBuilder.java:206 software.amazon.awssdk.core.internal.http.pipeline.stages.MakeHttpRequestStage.execute(Object, RequestExecutionContext)" avg_objects_sec="149" recorded_objects="352878" size="34672664">
<node call_tree="MakeHttpRequestStage.java:39 software.amazon.awssdk.core.internal.http.pipeline.stages.MakeHttpRequestStage.execute(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="149" recorded_objects="352878" size="34672664">
<node call_tree="MakeHttpRequestStage.java:55 software.amazon.awssdk.core.internal.http.pipeline.stages.MakeHttpRequestStage.executeHttpRequest(SdkHttpFullRequest, RequestExecutionContext)" avg_objects_sec="149" recorded_objects="352866" size="34672328">
<node call_tree="MakeHttpRequestStage.java:76 software.amazon.awssdk.core.internal.util.MetricUtils.measureDurationUnsafe(Callable)" avg_objects_sec="148" recorded_objects="351897" size="34640128">
<node call_tree="MetricUtils.java:64 software.amazon.awssdk.http.apache.ApacheHttpClient$1.call()" avg_objects_sec="148" recorded_objects="351884" size="34639816">
<node call_tree="ApacheHttpClient.java:229 software.amazon.awssdk.http.apache.ApacheHttpClient$1.call()" avg_objects_sec="148" recorded_objects="351884" size="34639816">
<node call_tree="ApacheHttpClient.java:232 software.amazon.awssdk.http.apache.ApacheHttpClient.access$500(ApacheHttpClient, HttpRequestBase)" avg_objects_sec="148" recorded_objects="351884" size="34639816">
<node call_tree="ApacheHttpClient.java:106 software.amazon.awssdk.http.apache.ApacheHttpClient.execute(HttpRequestBase)" avg_objects_sec="148" recorded_objects="351884" size="34639816">
<node call_tree="ApacheHttpClient.java:253 software.amazon.awssdk.http.apache.internal.impl.ApacheSdkHttpClient.execute(HttpUriRequest, HttpContext)" avg_objects_sec="148" recorded_objects="351135" size="34614656">
<node call_tree="ApacheSdkHttpClient.java:72 <...> org.apache.http.impl.client.CloseableHttpClient.execute(HttpUriRequest, HttpContext)" avg_objects_sec="148" recorded_objects="351135" size="34614656">
<node call_tree="<6 calls> software.amazon.awssdk.http.apache.internal.conn.ClientConnectionManagerFactory$Handler.invoke(Object, Method, Object[])" avg_objects_sec="146" recorded_objects="347451" size="31410272">
<node call_tree="ClientConnectionManagerFactory.java:80 <...> java.lang.reflect.Method.invoke(Object, Object[])" avg_objects_sec="146" recorded_objects="347451" size="31410272">
<node call_tree="<4 calls> DefaultHttpClientConnectionOperator.java:142 software.amazon.awssdk.http.apache.internal.conn.SdkTlsSocketFactory.connectSocket(int, Socket, HttpHost, InetSocketAddress, InetSocketAddress, HttpContext)" avg_objects_sec="146" recorded_objects="347334" size="31406824">
<node call_tree="SdkTlsSocketFactory.java:113 <...> org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(int, Socket, HttpHost, InetSocketAddress, InetSocketAddress, HttpContext)" avg_objects_sec="146" recorded_objects="347272" size="31404744">
<node call_tree="<1 call> SSLConnectionSocketFactory.java:434 software.amazon.awssdk.http.apache.internal.conn.SdkTlsSocketFactory.prepareSocket(SSLSocket)" avg_objects_sec="0" recorded_objects="109" size="3768">
<node call_tree="SdkTlsSocketFactory.java:86 <...> sun.security.ssl.SSLSocketImpl.setEnabledProtocols(String[])" avg_objects_sec="0" recorded_objects="42" size="1816"/>
<node call_tree="SdkTlsSocketFactory.java:59 <...> sun.security.ssl.SSLSocketImpl.getEnabledProtocols()" avg_objects_sec="0" recorded_objects="19" size="552"/>
<node call_tree="SdkTlsSocketFactory.java:58 <...> sun.security.ssl.SSLSocketImpl.getSupportedProtocols()" avg_objects_sec="0" recorded_objects="9" size="248"/>
<node call_tree="SdkTlsSocketFactory.java:87 <...> java.lang.invoke.Invokers$Holder.linkToTargetMethod(Object, Object)" avg_objects_sec="0" recorded_objects="8" size="128"/>
<node call_tree="SdkTlsSocketFactory.java:71 <...> java.util.ArrayList.add(Object)" avg_objects_sec="0" recorded_objects="8" size="448"/>
<node call_tree="SdkTlsSocketFactory.java:60 <...> java.lang.invoke.Invokers$Holder.linkToTargetMethod(Object, Object, Object)" avg_objects_sec="0" recorded_objects="6" size="144"/>
<node call_tree="SdkTlsSocketFactory.java:67 software.amazon.awssdk.http.apache.internal.conn.TlsProtocol.values()" avg_objects_sec="0" recorded_objects="6" size="192"/>
<node call_tree="SdkTlsSocketFactory.java:85" avg_objects_sec="0" recorded_objects="5" size="80"/>
<node call_tree="SdkTlsSocketFactory.java:63" avg_objects_sec="0" recorded_objects="4" size="96"/>
<node call_tree="SdkTlsSocketFactory.java:85 java.util.ArrayList.toArray(Object[])" avg_objects_sec="0" recorded_objects="2" size="64"/>
</node>
</node>
<node call_tree="SdkTlsSocketFactory.java:116 software.amazon.awssdk.http.apache.internal.net.SdkSslSocket.<init>(SSLSocket)" avg_objects_sec="0" recorded_objects="52" size="1760"/>
<node call_tree="SdkTlsSocketFactory.java:111 <...> java.lang.invoke.Invokers$Holder.linkToTargetMethod(Object, Object)" avg_objects_sec="0" recorded_objects="5" size="80"/>
<node call_tree="SdkTlsSocketFactory.java:116" avg_objects_sec="0" recorded_objects="5" size="240"/>
</node>
Stack traces
2020-11-19 10:26:40.083 ERROR 1 --- [ task-4] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected exception occurred invoking async method: public void com.company.Class.start()
java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.HashMap$KeySet.iterator(HashMap.java:985) ~[na:na]
at java.base/java.util.HashSet.iterator(HashSet.java:174) ~[na:na]
at java.base/sun.security.util.AbstractAlgorithmConstraints.checkAlgorithm(AbstractAlgorithmConstraints.java:103) ~[na:na]
at java.base/sun.security.util.DisabledAlgorithmConstraints.permits(DisabledAlgorithmConstraints.java:140) ~[na:na]
at java.base/sun.security.ssl.SSLAlgorithmConstraints.permits(SSLAlgorithmConstraints.java:164) ~[na:na]
at java.base/sun.security.ssl.SSLAlgorithmConstraints.permits(SSLAlgorithmConstraints.java:154) ~[na:na]
at java.base/sun.security.ssl.HandshakeContext.isActivatable(HandshakeContext.java:550) ~[na:na]
at java.base/sun.security.ssl.HandshakeContext.getActiveCipherSuites(HandshakeContext.java:339) ~[na:na]
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:185) ~[na:na]
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98) ~[na:na]
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:238) ~[na:na]
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:430) ~[na:na]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:436) ~[httpclient-4.5.13.jar!/:4.5.13]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:384) ~[httpclient-4.5.13.jar!/:4.5.13]
at software.amazon.awssdk.http.apache.internal.conn.SdkTlsSocketFactory.connectSocket(SdkTlsSocketFactory.java:113) ~[apache-client-2.15.31.jar!/:na]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142) ~[httpclient-4.5.13.jar!/:4.5.13]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376) ~[httpclient-4.5.13.jar!/:4.5.13]
at jdk.internal.reflect.GeneratedMethodAccessor67.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at software.amazon.awssdk.http.apache.internal.conn.ClientConnectionManagerFactory$Handler.invoke(ClientConnectionManagerFactory.java:80) ~[apache-client-2.15.31.jar!/:na]
at com.sun.proxy.$Proxy109.connect(Unknown Source) ~[na:na]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393) ~[httpclient-4.5.13.jar!/:4.5.13]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) ~[httpclient-4.5.13.jar!/:4.5.13]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) ~[httpclient-4.5.13.jar!/:4.5.13]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.13.jar!/:4.5.13]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.13.jar!/:4.5.13]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.13.jar!/:4.5.13]
at software.amazon.awssdk.http.apache.internal.impl.ApacheSdkHttpClient.execute(ApacheSdkHttpClient.java:72) ~[apache-client-2.15.31.jar!/:na]
at software.amazon.awssdk.http.apache.ApacheHttpClient.execute(ApacheHttpClient.java:253) ~[apache-client-2.15.31.jar!/:na]
at software.amazon.awssdk.http.apache.ApacheHttpClient.access$500(ApacheHttpClient.java:106) ~[apache-client-2.15.31.jar!/:na]
at software.amazon.awssdk.http.apache.ApacheHttpClient$1.call(ApacheHttpClient.java:232) ~[apache-client-2.15.31.jar!/:na]
Steps to Reproduce
The application downlaods files from a API and save it into S3 Bucket. it saves around 1000 files per a minute into S3 Bucket and runs around 10 min and dying. My application Sample Code below
@Component
public class S3WriterImpl implements FileWriter {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final Pattern s3BucketPattern = Pattern.compile("^(s3://[\\w\\-]+)(/)(.*)");
@Value("${save.enableGzip}")
private boolean enableGzip;
@Value("${aws.s3.sse}")
private String sse;
@Value("${aws.s3.ssekmsKeyId}")
private String ssekmsKeyId;
@Value("${aws.s3.testBucket}")
private String s3TestBucket;
@Value("${aws.useEc2InstanceProfile}")
private boolean useEc2InstanceProfile;
@Value("${aws.s3.useTestBucket}")
private boolean useTestBucket;
@Autowired
Environment env;
@Override
public void writeFile(byte[] content, String directory, String filename) {
Matcher s3BucketMatcher = s3BucketPattern.matcher(directory);
s3BucketMatcher.find();
String bucketName = getBucketName(s3BucketMatcher);
String objectKey = s3BucketMatcher.group(3) + "/" + (enableGzip ? filename + ".gz" : filename);
logger.debug("writeFileToS3: " + bucketName + "/" + objectKey + " , content length is " + (content != null ? content.length : "NULL"));
S3Client s3 = getS3Client();
try {
byte[] data = getData(content);
//Put a file into the bucket
PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
.serverSideEncryption(sse)
.ssekmsKeyId(ssekmsKeyId)
.bucket(bucketName)
.key(objectKey)
.build(),
RequestBody.fromBytes(data));
} catch (Exception e) {
throw new RuntimeException(String.format("Write to s3 fails for file %s in path %s", filename, directory), e);
}
}
private byte[] getData(byte[] content) throws IOException {
byte[] returnValue;
if (enableGzip) {
try(ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
) {
gzipOutputStream.write(content);
gzipOutputStream.flush();
gzipOutputStream.close();
returnValue = byteArrayOutputStream.toByteArray();
}
} else {
try(ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();) {
byteArrayOutputStream.write(content);
byteArrayOutputStream.close();
returnValue = byteArrayOutputStream.toByteArray();
}
}
return returnValue;
}
/**
* If the active profile contains neither "2local" or "2integration-test" , return S3Client which
* uses Amazon EC2 instance profiles.
* <br/>
* If the active profile contains "2local" or "2integration-test", return default S3Client.
* <br/>
* default S3Client will scan AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY variabls to access to S3 Bucket.
* @return
*/
private S3Client getS3Client() {
S3ClientBuilder s3ClientBuilder = S3Client.builder().region(Region.EU_CENTRAL_1);
if(useEc2InstanceProfile){
s3ClientBuilder.credentialsProvider(InstanceProfileCredentialsProvider.builder().build());
}
return s3ClientBuilder.build();
}
private String getBucketName(Matcher s3BucketMatcher) {
String bucketName = s3BucketMatcher.group(1).replaceFirst("^[sS]3://", "");
// If the Production Profile is not active, force to use Test bucket
if(useTestBucket){
bucketName = s3TestBucket;
}
return bucketName;
}
}
Possible Solution
Context
The application runs and keep downloading files from APIs and save it into S3 Bucket. when the application saves files for a while and the application stops due to the error. It worked fined when the application saved in our HDFS. I am migrating the application that saves into S3 Bucket instead of HDFS. Since AWS SDK in use, the application stops every 10 min.
Your Environment
- AWS Java SDK version used: 2.15.31
- JDK version used: openjdk 15.0.1
- Operating System and version: Linux / Windows (local)