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

Holding onto objects too long #1

Open
PH0lder opened this issue Feb 6, 2025 · 1 comment
Open

Holding onto objects too long #1

PH0lder opened this issue Feb 6, 2025 · 1 comment

Comments

@PH0lder
Copy link

PH0lder commented Feb 6, 2025

If a person were to make a large circular buffer, and fill it up and subsequently empty it, all the objects placed into it have a strong reference, and thus can't be garbage collected, until they're overwritten by new objects which will then have the same problem. To fix this, your get() method should assign the element in the array to null when the object is returned.

@adamretter
Copy link
Member

adamretter commented Feb 25, 2025

@PH0lder I have had a look at this but I am not sure I can do that without breaking the existing behaviour of get on an Unordered Ring Buffer.

For example, if I had the suggested entries[readIdx] = null; at line 169 of RingBuffer.java, then the test in RingBufferTest.java: putGetPutGetUnordered(7, 10, 2, 2, 16) shows the following behaviour:

capacity(7):

 0, 1, 2, 3, 4, 5, 6
[N, N, N, N, N, N, N]

put1(10):

[a, b, c, d, e, f, g]
[h, i, j            ]
=>
[h, i, j, d, e, f, g]

get1(2):

[N, N, j, d, e, f, g]

put2(2):

[N, N, j, d, e, f, g]
[         a, b      ]
=>
[N, N, j, a, b, f, g]

get2(16):

[N, N, j, a, b, f, g]

The second get operation (get2) is now returning the wrong results, it should have returned: [a, b, f, g, h, i, j]

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