You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/JavaCodePerformance.md
+6-1Lines changed: 6 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2080,6 +2080,10 @@ This especially applies to direct file streaming. Access through ClassLoader.get
2080
2080
2081
2081
**Observation: All bytes of a large file are loaded into memory.** E.g. for uploading or downloading documents, for instance to determine the mime type or create a digest.
2082
2082
**Problem:** Large objects are allocated on the heap, up to e.g. 50 MB. We also observed 300 MB and even 1 GB in back-end systems. This likely triggers long gc pauses for compaction or may trigger out of memory crashes.
2083
+
APIs like `Files.readAllBytes`, `Files.readAllLines`, `Files.readString`, `InputStream.readAllBytes`, `IOUtils.toByteArray`, `ByteArrayInputStream`,
2084
+
`ByteArrayOutputStream.toByteArray`, and `ResizableByteArrayOutputStream.toByteArray` all load entire content into heap memory.
2085
+
This risks OutOfMemoryError, long GC pauses, and slow responses when processing large files.
2086
+
**Note:** Not a problem for small files.
2083
2087
2084
2088
In the next example, there are two large byte arrays in memory: one in baos and the other is the returned byte array since a copy is made in toByteArray().
2085
2089
@@ -2102,7 +2106,8 @@ class Bad1 {
2102
2106
}
2103
2107
```
2104
2108
2105
-
**Solution:** Stream-through: use streaming all the way, don't store the whole thing in memory, don't use byte arrays. A mime type is determined from the first few bytes of the file, don't read in all 50MB-1GBfor that. Often, functionality can be achieved in a streaming way, i.e. [a digest can be computed in a streaming way](http://www.mkyong.com/java/java-sha-hashing-example/).
2109
+
**Solution:** Use streaming APIs end-to-end, do not buffer whole files in memory.
2110
+
A mime type is determined from the first few bytes of the file, don't read in all 50MB-1GBfor that. Usually, functionality can be achieved in a streaming way, i.e. [a digest can be computed in a streaming way](http://www.mkyong.com/java/java-sha-hashing-example/).
<description>Problem: for connectionRequestTimeout, connectTimeout, socketTimeout (using
6128
-
HttpComponentsClientHttpRequestFactory/RequestConfig) or readTimeout/responseTimeout (using RequestConfig v4/v5) and connectTimeout (using ConnectionConfig v5)
6129
-
the default timeout settings are not optimal in most cases.
6130
-
Solution: Set the timeouts explicitly to proper reasoned values. See best practice values via the link. Use
6140
+
<description>Problem: default timeout values for Apache HttpClient are often sub optimal (connectionRequestTimeout, connectTimeout, socketTimeout in HttpComponentsClientHttpRequestFactory;
6141
+
readTimeout/responseTimeout in RequestConfig v4/v5; and connectTimeout in ConnectionConfig v5)
6142
+
Solution: Set all these timeouts explicitly to proper reasoned values. See best practice values via the link. Use
6131
6143
the setDefaultRequestConfig with a method with a RequestConfig object on HttpClient builders to set the
<description>Problem: for connectionRequestTimeout, connectTimeout, socketTimeout (using
6128
-
HttpComponentsClientHttpRequestFactory/RequestConfig) or readTimeout/responseTimeout (using RequestConfig v4/v5) and connectTimeout (using ConnectionConfig v5)
6129
-
the default timeout settings are not optimal in most cases.
6130
-
Solution: Set the timeouts explicitly to proper reasoned values. See best practice values via the link. Use
6140
+
<description>Problem: default timeout values for Apache HttpClient are often sub optimal (connectionRequestTimeout, connectTimeout, socketTimeout in HttpComponentsClientHttpRequestFactory;
6141
+
readTimeout/responseTimeout in RequestConfig v4/v5; and connectTimeout in ConnectionConfig v5)
6142
+
Solution: Set all these timeouts explicitly to proper reasoned values. See best practice values via the link. Use
6131
6143
the setDefaultRequestConfig with a method with a RequestConfig object on HttpClient builders to set the
0 commit comments