Skip to content

Conversation

sabrinajlee
Copy link
Collaborator

@sabrinajlee sabrinajlee commented Oct 14, 2025

The cleaner is the replacement for the deprecated finalize() method for cleaning objects before garbage collection.

Each class that was previously using finalize() will have an anonymous function that cleans up the necessary resources. The number of cleaner threads can be set by a property with the default value being 2. The object and its anonymous function are registered to one of the cleaner threads in round robin order.

Signed-off-by: Sabrina Lee [email protected]

@jasonkatonica
Copy link
Member

I believe your DCO in the commit text and in the PR description should read:

Signed-off-by: Sabrina Lee <[email protected]>

Instead of

Signed-off by: Sabrina Lee <[email protected]>

Notice the - before the by above.

This might explain why the DCO check has failed.

@sabrinajlee sabrinajlee force-pushed the main-multicleaner branch 2 times, most recently from ae88020 to 1d79d60 Compare October 14, 2025 21:54
@KostasTsiounis KostasTsiounis self-requested a review October 14, 2025 23:51
Copy link
Member

@KostasTsiounis KostasTsiounis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}
}
} catch (OCKException e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit worried about printing stack traces on a mass scale if something goes wrong here but not sure we have any other options?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to limit how many times the stack trace prints with a counter inside the catch block? Since it could be more than a one time issue but if the cleaning is failing so frequently that it enters this catch block more than a certain number of times it will run into OOM so there should be a limit to how many times we think it needs to print.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a reasonable approach to me and better then any idea that i have at the moment. I assume we cannot throw an exception from this method at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can throw an exception just fine. The question is do you want to fail if it happens once?

}

public void registerCleanable(Object owner, Runnable cleanAction) {
Cleaner cleaner = cleaners[Math.abs(count.getAndIncrement()) % numCleaners];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think we should validate that this works ok in and around the max integer of 2,147,483,648

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested out Math.floorMod() as well as resetting the count variable at Integer.MAX_VALUE and the latter seems like the better option because we can reset the value so that the next consecutive cleaner thread in the array is used, where the Math.floorMod() that depends on the number of cleaner threads and we can't control it. In either case the default number of cleaners (2) is fine but for a different number of cleaners the distribution of cleaners would become imbalanced over time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK seems reasonable to me to just reset the counter once we reach Integer.MAX_VALUE. I think the code will be more easy to understand also.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Math.floorMod() is not useful in integer calculations anyway. The check and resetting can be a bit time consuming, especially in an AtomicInteger. I don't see what the problem with using abs() is.

Copy link
Member

@jasonkatonica jasonkatonica Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is that abs() of Integer.MAX_VALUE + 1 returns a negative value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abs() on Integer.MIN_VALUE returns a negative value, and then the % operator in Java works differently than in math, normally modulo should always return a positive number but in Java using % on a negative operand returns a negative value.

The cleaner is the replacement for the deprecated finalize() method for
cleaning objects before garbage collection.

Each class that was previously using finalize() will have an anonymous
function that cleans up the necessary resources. The number of cleaner
threads can be set by a property with the default value being 2. The
object and its anonymous function are registered to one of the cleaner
threads in round robin order.

Signed-off-by: Sabrina Lee <[email protected]>

abstract void setOCKExceptionCause(Exception exception, Throwable ockException);

private static class CleanerThreadFactory implements ThreadFactory {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're not gonna change anything in the thread, then the custom ThreadFactory is not needed at all and can be removed.

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

Successfully merging this pull request may close these issues.

3 participants