-
Notifications
You must be signed in to change notification settings - Fork 92
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
FluentLogFactory: Why use WeakHashMap to store loggers? #37
Comments
@todesking Thanks for your report. @muga Why did you change the implementation of Map from HashMap to WeakHashMap at d5efd4d? |
@muga Any updates? |
@todesking We've used WeakHashMap and finalize method for reducing the heap space consumption. I thought and considered the possibility that some users create a lot of FluentLogger objects on his JVM. But the implementation is not good in some cases, you know. @komamitsu We should remove FluentLoggerFactory (and the WeakHashMap impl.) from external API. FluentLogger objects and the object creations should be managed by users. |
@todesking good catch, thanks for your report.
I prefer to use 1. for the backward compatibility. Also, users can choose whether they manage the object by themselves or by FluentLoggerFactory. For handling small number of the objects, it's enough. |
@muga @oza Thanks for your ideas. For option 1, I'm concerned about memory consumption because all FluentLoggers stored in HashMap are never released. I think option 2 is simple and isn't confusable. Of course, backward compatibility is important, though as you said.
FluentLogger internally depends on FluentLogFactory. https://github.com/fluent/fluent-logger-java/blob/master/src/main/java/org/fluentd/logger/FluentLogger.java#L32. So we need to remove the dependency from FluentLogger if we choose this option. |
@komamitsu Rethinking of this - how about having the key of WeakHashMap at FluentLogger via FluentLogger#setFactoryKey or something to prevent the GC? When a instance of FluentLogger#close() |
@oza Make sense. I think the cause of this problem is the way of holding a reference of FluentLogger in FluentLoggerFactory. I created a PR (#38) for it. The PR has the following little downsides. But I think they are ignorable.
What do you think? |
@komamitsu I agree with your idea. @muga what do you think? |
@komamitsu @oza thank you for driving this issue and fixing. #38 looks good to me to fix this issue. compatibility is important:-) |
@muga @komamitsu Merged #38. @todesking Thanks for your reporting! |
The last assertion is sometimes failed. That because
WeakHashMap
has weak-referenced keys and strong-referenced values, so when key(String
generated inFluentLoggerFactory#getLogger()
) is GCed, correspond logger is removed from the map.Because of this,
FluentLogerFactory#closeAll()
andflushAll()
has unexpected behaviour: Some loggers are closed/flushed, and rest is not touched.It seems just a bug and use normal
HashMap
to fix it. Is this correct?The text was updated successfully, but these errors were encountered: