What's the overhead of creating a SLF4J loggers in static vs. non-static contexts?

后端 未结 2 1265
情话喂你
情话喂你 2020-12-29 04:14

I\'ve always used the following pattern to construct (SLF4J) loggers:

private static final Logger log = LoggerFactory.getLogger(MyClass.class);
2条回答
  •  感动是毒
    2020-12-29 05:02

    The overhead for non-static (instance) logger variables should be negligible unless many, say 10000 or more, instantiations occur. The key word here is negligible. If many (>10000) objects are instantiated, the impact will probably be measurable but still be low.

    More specifically, an instance logger increases the memory footprint by one reference (64 bits) per object instance. On the CPU side, the cost is one hash look up per instance, i.e. the cost of looking up the appropriate logger in a hash table (small). Again, both costs should be negligible unless many many objects are created.

    This question is also discussed in the SLF4J FAQ.

提交回复
热议问题