RootEncoder
RootEncoder copied to clipboard
gc

it calls gc Every 5 seconds,Is this right?
I'm not calling gc in the code it is called automatically. If you find a way to optimize it or you have any suggestion to solve the problem let me know it and we can disccuss about it.
Have you used similar code?
byte[] buffer = new byte[4096];
while (***) {
file.write(buffer, 0, bytesRead);
}
If it exists, please refer to the following code:
class ByteArrayPool {
private final Queue<byte[]> pool = new LinkedList<>();
private final int maxPoolSize;
ByteArrayPool(int maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
byte[] getBuf(int len) {
synchronized (pool) {
while (!pool.isEmpty()) {
byte[] buf = pool.remove();
if (buf.length >= len) {
return buf;
}
}
}
return new byte[len];
}
void returnBuf(byte[] buf) {
synchronized (pool) {
if (pool.size() < maxPoolSize) {
pool.add(buf);
}
}
}
}
ByteArrayPool pool = new ByteArrayPool(5);
int bytesRead;
while ((bytesRead = httpResponse.body().byteStream().read(pool.getBuf(4096))) != -1) {
file.write(pool.getBuf(bytesRead), 0, bytesRead);
downloadedBytes += bytesRead;
}
or :
ByteBuffer buffer = ByteBuffer.allocate(4096);
int bytesRead;
while ((bytesRead = httpResponse.body().byteStream().read(buffer)) != -1) {
buffer.flip();
file.write(buffer.array(), 0, bytesRead);
buffer.clear();
downloadedBytes += bytesRead;
}