日志系列之Log4j2 Garbage-free模式

来源:互联网 发布:ui设计软件下载 编辑:程序博客网 时间:2024/06/10 02:28

Garbage-free核心思想:避免或减少对象的创建;

实现方式

  1. 对象复用(threadlocal、jvm级别复用);
  2. 避免临时对象的创建;

From version 2.6, Log4j runs in “garbage free” mode by default where objects and buffers are reused and no temporary objects are allocated as much as possible. There is also a “low garbage” mode which is not completely garbage free but does not use ThreadLocal fields. This is the default mode when Log4j detects it is running in a web application.

检测web应用

  方式:检测javax.servlet.Servlet是否在classpath上;

系统参数配置

  • log4j2.enableThreadlocals - if “true” (the default for non-web applications) objects are stored in ThreadLocal fields and reused, otherwise new objects are created for each log event.
  • log4j2.enableDirectEncoders - if “true” (the default) log events are converted to text and this text is converted to bytes without creating temporary objects.
原创粉丝点击