Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the length of the buffer should grow double times than previous #24

Open
LinboLen opened this issue Jan 11, 2017 · 1 comment
Open

the length of the buffer should grow double times than previous #24

LinboLen opened this issue Jan 11, 2017 · 1 comment

Comments

@LinboLen
Copy link

LinboLen commented Jan 11, 2017

the worsest scenario is that, append 1 byte repeatly.
if total buffer size finally is 1MBtye, write 1 byte one by one
then the waste memory is that ((1M-1) + 1)(1M-1) / 2 ≈ 0.5TByte, if total buffer size is N Byte, the waste memory is almost equal N^2/2,
the memory leak is scary

the best way to increse buffer size is use doule times increase,
so if total buffer size finally is 1MBtye+1, write bytes free of hands.
then the waste memory is that (1MByte-1) + 1Mbyte*2 -1(the unused memory + 1b+11b+111b+1111b+...+1M) ≈ 3MByte

the waste uint8array class instatiate calculate is the same

ref: https://github.com/openjdk-mirror/jdk/blob/jdk8u/jdk8u/master/src/share/classes/java/io/ByteArrayOutputStream.java#L113
java use double times buffer size increase

@LinboLen LinboLen changed the title the length of the buffer should grow double times than previous buffer length the length of the buffer should grow double times than previous Jan 11, 2017
@timkurvers
Copy link
Owner

Hi and thanks! Ouch, that sounds scary and a very reasonable approach.

I'll gladly accept a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants