RootEncoder icon indicating copy to clipboard operation
RootEncoder copied to clipboard

gc

Open lijiaqq opened this issue 3 years ago • 2 comments

316411655091464_ pic

it calls gc Every 5 seconds,Is this right?

lijiaqq avatar Jun 13 '22 04:06 lijiaqq

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.

pedroSG94 avatar Jun 13 '22 15:06 pedroSG94

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;
}

Reginer avatar Feb 25 '23 03:02 Reginer