Tuesday, 24 October 2017

Allocation free logging with log4j2

Introduction

Recently I was working for a client trying to remove some GC pauses for a large well crafted Java system. After profiling I realised that most of the garbage was being produced by logging!! Was there a simple unobtrusive way to remove all that allocation?  Turns out there was :)

Which framework should I use for GC free logging?


Let's turn our attention back to the age old question of which logging framework to use.

The usual candidates are
  • log4j2
  • logback
  • java.util.logging
  • slf4j (simple)
and unless you are doing something super funky you might not think it makes a huge difference which one you choose.

There have been many studies done comparing performance and those of course this can be important if you are playing in the ultra low latency arena. (see Benchmarking Java logging frameworks)


But what I'm concerned about is allocation!!

See my earlier post on the evils of allocation The First Rule of Performance Optimisation.  

In a typical system 30%-50% of all allocation can be in logging!!

So even if most people don't mind if logging takes a couple of milliseconds longer in one framework than another they will almost certainly care about long GC pauses caused by the garbage created by the logging framework.

And all this logging allocation can be removed by simple configuration.

Log4J2 from 2.6 onwards is allocation free so really no excuse not to take advantage of it :) 

Let's see it in action

It's one thing making the allocation free claim but let's see if this holds true in practice.

Consider this simple logging code below: Note that I've used slf4j in the code and therefore only need change the configuration to run with the different logging frameworks.  It's a good idea to do this because although log4j2 might be the best framework at the moment who knows what tomorrow will bring...



Run with log4j2 - no allocation

If we configure the program to run with log4j2 using this maven configuration:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.9.1</version>
</dependency>

And we use this log4j2 configuration

xml version="1.0" encoding="UTF-8" ?><Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss:SSS} [%t] %-5level %logger[36] %msg%n"></PatternLayout>
        </Console>
        <File name="MyFile" fileName="all.log" immeadiateFlush="false" append="false">
            <PatternLayout pattern="%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5level %logger[36] %msg%n"></PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            
            <AppenderRef ref="MyFile"></AppenderRef>
        </Root>
    </Loggers>
</Configuration>

Then we get 0 allocation!

We can prove this by running Flight Recorder on the program (see below):

The only memory being allocated is due to Flight Recorder (by the way FR can be configured so that this doesn't happen).



Run using logback

Try exactly the same this using Logback.

Use this Maven configuration

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

Use this logback configuration:

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>myApp.log</file>
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>
    <root level="debug">
        <appender-ref ref="FILE" />
        
    </root>
</configuration>

And when we run with Flight Recorder we see a hug amount allocation!


But a word of warning....

You need to use the log4j2 configuration exactly as specified in the documentation see supported layouts.  If you even change date format slightly allocation will again be rampant.

Summary

  • Use slf4j so that you can easily change your logging implementation
  • Use log4j2 to avoid allocation
  • Make sure you use the supported formats to support allocation free logging

167 comments:

  1. While working logging can cause a lot of errors. You have to be perfect so your program can be error less. Thank you for sharing. This was very helpful.

    ReplyDelete
  2. Today, we can’t simply ignore the importance of the video and movie streaming applications as we have been more dependent on our smartphones. And talking about the video streaming applications, SnapTube is by far the most famous and downloaded streaming application of the current time. So want more insight on the SnapTube app? Check out this website Download Snaptube Latest Apk

    ReplyDelete
  3. https://www.learndigital.co/ sign up to learn digital marketing.

    ReplyDelete
  4. Amazing Article ! I have bookmarked this article page as i received good information from this. All the best for the upcoming articles.

    Tekslate Online Training's

    ReplyDelete
  5. hi, this is Really a great post. Appreciate the effort in educating us. Thanks for it.
    DevOps Online Training

    ReplyDelete
  6. it is really explainable very well and i got more information from your blog.
    shareplex training
    sharepoint Training

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hey gays how are you.Please Download And enjoy the software Movavi Video Converter Activation Key

    ReplyDelete
  9. I see there are many dead trigger 2 mod apk 1.3.1 unlmiited money people who are into things hungry shark world unlimited gem that are pretty much jnc311 review not liked by others but people do visit it so much games download for android mobile touch screen

    ReplyDelete
  10. Interesting information and attractive.This blog is really rocking... Yes, the post is very interesting and I really like it.I never seen articles like this. I meant it's so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job.
    Kindly visit us @
    Sathya Online Shopping
    Online AC Price | Air Conditioner Online | AC Offers Online | AC Online Shopping
    Inverter AC | Best Inverter AC | Inverter Split AC
    Buy Split AC Online | Best Split AC | Split AC Online
    LED TV Sale | Buy LED TV Online | Smart LED TV | LED TV Price
    Laptop Price | Laptops for Sale | Buy Laptop | Buy Laptop Online
    Full HD TV Price | LED HD TV Price
    Buy Ultra HD TV | Buy Ultra HD TV Online
    Buy Mobile Online | Buy Smartphone Online in India

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. The article is very interesting and very understood to be read, may be useful for the people. I wanted to thank you for this great read!! I definitely enjoyed every little bit of it. I have to bookmarked to check out new stuff on your post. Thanks for sharing the information keep updating, looking forward for more posts..
    Kindly visit us @
    Madurai Travels
    Best Travels in Madurai
    Cabs in Madurai
    Tours and Travels in Madurai

    ReplyDelete
  13. Excellent Blog. I really want to admire the quality of this post. I like the way of your presentation of ideas, views and valuable content. No doubt you are doing great work. I’ll be waiting for your next post. Thanks .Keep it up! Kindly visit us @ Christmas Gift Boxes | Wallet Box | Perfume Box Manufacturer
    Candle Packaging Boxes
    Luxury Leather Box
    Luxury Clothes Box
    Luxury Cosmetics Box
    Shoe Box Manufacturer
    Luxury Watch Box

    ReplyDelete
  14. An amazing web journal I visit this blog, it's unbelievably wonderful. Oddly, in this blog's content made without a doubt and reasonable. The substance of data is informative.
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  15. Wow, what an awesome spot to spend hours and hours! It's beautiful and I'm also surprised that you had it all to yourselves! we are the one of the best HIV treatment hospital in India..Click here for more details Best HIV Treatment in India | Top HIV Hospital in India | HIV AIDS Treatment in Mumbai | HIV Specialist in Bangalore | HIV Positive Treatment in India | Medicine for AIDS in India

    ReplyDelete

  16. Sports big news provide is a all type sports contents. In This portal is a sports all type content, For ex. Cricket, cwc19, UEFA, football, Copa America 2019 etc.

    Cricket
    Cricket World Cup 2019
    Live Streaming
    World Cup 2019 Final
    IPL 2020 Live Streaming
    Copa America 2019 Venues
    Tennis
    Copa America 2019
    Cricket World Cup Live
    Football

    ReplyDelete
  17. hallo pecinta game android, ada game seru ini untuk kamu mainkan bersama teman-teman kamu. Yang pasti game yang berjudul dead trigger 2 ini tidak kalah serunya dari game yang lainnya. Kami telah menyediakan review tetang game ini dengan lengkap beserta link downloadnya. Kamu bisa download game dead trigger 2 ini secara gratis offline disini. Jika kamu penasaran tentang keseruan game dead trigger 2 andorid apk obb ini silahkan mampir ke blog android saya ya. Baca selengkapnya: dead trigger 2 hack apk. link: https://downloadgameofflineapkmod.blogspot.com/2019/07/dead-trigger-2-offline-version-apk-obb.html

    ReplyDelete
  18. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Blockchain online training

    ReplyDelete
  19. nice article
    Unicsol offers Best full stack developer course in hyderabadget trained by 10+years of experienced faculty and get placed as full stack developer.

    ReplyDelete
  20. Nice post. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated posts…
    Devops online training
    Devops certification training
    Devops online course
    Devops training course

    ReplyDelete
  21. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Selenium online training
    Selenium certification training
    Selenium online course
    Selenium training course

    ReplyDelete
  22. The most sacred place National War museum Delhi is inaugurated now in the nearby vicinity of India Gate . Here in the article we will help you out in solving our signature query how to reach National War memorial Delhi nearby India Gate . Along with that we will also sort out few other queries related to the National War memorial Delhi like nearest metro station, war memorial Delhi timing and also nearest metro stations to India Gate .

    ReplyDelete
  23. Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
    SEO Training in Chennai
    SEO Training in Bangalore
    SEO Training in Coimbatore
    SEO Training in Madurai

    ReplyDelete

  24. Nuvigil smart drug is the trade name for Armodafinil smart drug and is the enantiopure compound of Modafinil. There are a number of health benefits that the use of Nuvigil smart drug has to offer to its users. So, you can buy Nuvigil online. The use of the smart drug is safe and has no health issues attached to its usage. In the year 2006, the FDA approved the use of generic Nuvigil smart drug to be used to treat a number of mental issues. Buy Nuvigil online as the use of Nuvigil smart drug has long effect when compared to other smart drugs. This is mainly because Armodafinil smart drug has a longer half-life than the other smart drugs. Therefore more and more people are depending on Nuvigil smart drug to carry on their daily routine smoothly.
    Buy Nuvigil online

    ReplyDelete
  25. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    Salesforce online training
    Hadoop online training

    ReplyDelete
  26. Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.

    Best Google cloud Online Certification

    ReplyDelete
  27. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article. blockchain online training

    ReplyDelete
  28. I am very impressive to read this post thanks for sharing this very good information.
    Events Planner In Delhi
    Wedding Planner In Delhi

    ReplyDelete
  29. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    best workday studio online training

    ReplyDelete
  30. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article. best devops online training

    ReplyDelete
  31. This comment has been removed by the author.

    ReplyDelete
  32. This is an amazing blog, thank you so much for sharing such valuable information with us.
    Visit for best website design and SEO services at- Website Development Company in India
    best website design services in gurgaon
    web company in delhi
    web desiging company
    web design & development banner
    web design & development company
    web design & development services
    web design agency delhi
    web design agency in delhi
    web design and development services
    web design companies in delhi
    web design company delhi
    web design company in delhi
    web design company in gurgaon
    web design company in noida
    web design company list
    web design company services
    web design company website
    web design delhi
    web design development company
    web design development services
    web design in delhi
    web design service
    web design services company
    web design services in delhi
    web designer company
    web designer delhi
    web designer in delhi
    web designers delhi
    web designers in delhi
    web designing & development
    web designing advertisement
    web designing and development
    web designing and development company
    web designing and development services
    web designing companies in delhi
    web designing company delhi
    web designing company in delhi
    web designing company in gurgaon
    web designing company in new delhi
    web designing company in noida
    web designing company logo
    web designing company sites
    web designing company websites
    web designing delhi
    web designing in delhi
    web designing in gurgaon
    web designing in gurgaon
    web designing service
    web designing services
    web designing services delhi

    ReplyDelete
  33. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  34. Nice blog.Thanks for sharing this information.
    If you want to know about latest fashion then please visit my site:-

    thefashionlifestyle

    ReplyDelete
  35. This is a great post and it gave me a lot of useful information. Salesforce Training Sydney

    ReplyDelete
  36. Great article ...Thanks for your great information, the contents are quiet interesting. I will be waiting for your next post.
    Microservices Online Training
    Microservices Training in Hyderabad

    ReplyDelete

  37. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear.i also want to inform you the best salesforce cpq training . thankyou . keep sharing..

    ReplyDelete
  38. Great efforts put to find the list of articles that are very useful to know. I’m thoroughly enjoying your blog. And Good comments create relations. You’re doing great work. Keep it up.

    Magento Development Training Course in Chennai Zuan Education

    Selenium Training Course in Chennai Zuan Education

    ReplyDelete
  39. Poker online situs terbaik yang kini dapat dimainkan seperti Bandar Poker yang menyediakan beberapa situs lainnya seperti http://62.171.128.49/hondaqq/ , kemudian http://62.171.128.49/gesitqq/, http://62.171.128.49/gelangqq/, dan http://62.171.128.49/seniqq. yang paling akhir yaitu http://62.171.128.49/pokerwalet/. Jangan lupa mendaftar di panenqq silakan dicoba bosku serta salam hoki

    ReplyDelete
  40. I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
    Rani Durgavati VishwaVidhyalaya BCOM TimeTable 2020

    ReplyDelete
  41. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    Java Training in Chennai

    ReplyDelete
  42. AWS
    Mind Q Systems provides python training in Hyderabad & Bangalore. Python Training designed for students and professionals. Mind Q Provides 100% placement assistance with python training.

    Mind Q Systems is a Software Training Institute in Hyderabad and Bangalore offering courses on Testing tools, selenium, java, oracle, Manual Testing, Angular, Python, SAP, Devops etc.to Job Seekers, Professionals, Business Owners, and Students. We have highly qualified trainers with years of real-time experience.


    ReplyDelete
  43. Thanks for posting
    my website techwolk is related to technology

    ReplyDelete
  44. Thanks for posting
    my webside techwolk.in/Sonsindiasong
    I have Webside techwolk.in/Sonsindiasong for Related by All type Songs.

    ReplyDelete
  45. This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work.satta king

    ReplyDelete
  46. A round of applause for your article.Much thanks again. Really Cool. Satta king

    ReplyDelete
  47. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    workday studio online training
    best workday studio online training
    top workday studio online training


    ReplyDelete

  48. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. ExcelR Digital Marketing Course Pune Any way I’ll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.

    ReplyDelete

  49. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    blockchain online training
    best blockchain online training
    top blockchain online training

    ReplyDelete
  50. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    workday studio online training
    best workday studio online training
    top workday studio online training

    ReplyDelete
  51. We have the best Kumbhalgarh Wildlife Sanctuary Safari Booking Stories Tag Archives: Kumbhalgarh Wildlife Sanctuary booking

    ReplyDelete
  52. I am from offering all type of data extractor in very low price you can buy at shuok just dial page i am sharing below:
    Justdial data extractor: https://youtu.be/f8_7CVjbyMM and https://youtu.be/psw0TKtv5wA
    G map data extractor: https://youtu.be/LTx13l9WWllvf’]\w
    Indiamart Data Extractor: https://youtu.be/5a8_II1DF1Q
    Tradeindia Data Extractor: https://youtu.be/_O5BDcKpzU4

    ReplyDelete
  53. Great post You are sharing much information keep it up ..awesome post


    I am sharing biography of top 10 businesses of india. Check my link for more informati
    on.

    ReplyDelete
  54. Great post You are sharing much information keep it up ..awesome post




    I am sharing <biography of top-10-richest-actor-in-india Check my link for more information.

    ReplyDelete
  55. I really happy found this website eventually. Really informative and inoperative, Thanks for the post and effort! Please keep sharing more such blog.

    Indian Home Remedies


    ReplyDelete

  56. I am very happy when read this blog post because blog post written in good manner and write on good topic. Java training in chennai Thanks for sharing valuable information.

    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  57. Thanks for sharing this informations.
    ios training in coimbatore

    amazon web services training in coimbatore

    aws training in coimbatore

    big data training in coimbatore

    hadoop training in coimbatore

    C and C++ training in coimbatore

    RPA Course in coimbatore

    ReplyDelete
  58. I have read your article, it is very informative and helpful for me. I admire the valuable information you offer in your articles. Thanks for posting it. I also would like to share some information about protecting against COVID-19 with Infection Control Face Shield.

    ReplyDelete
  59. Get real time project based and job oriented Salesforce training India course materials for Salesforce Certification with securing a practice org, database terminology, admin and user interface navigation and custom fields creation, reports & analytics, security, customization, automation and web to lead forms.  

    ReplyDelete
  60. I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.
    I have found this Salesforce training in India worth joining course. Try this Salesforce training in Hyderabad with job assistance. Join Salesforce training institutes in ameerpet with certification. Enroll for Salesforce online training in hyderabad with hands on course. 

    ReplyDelete
  61. If I had to give a great example of top quality content, this article would be it. It's a well-written commentary that holds your interest.
    SAP training in Kolkata
    SAP training Kolkata
    Best SAP training in Kolkata
    SAP course in Kolkata
    SAP training institute Kolkata

    ReplyDelete
  62. Myself so glad to establish your blog entry since it's actually quite instructive. If it's not too much trouble continue composing this sort of web journals and I normally visit this blog. Examine my administrations.
    Go through these Salesforce Lightning Features course. Found this Salesforce CRM Using Apex And Visualforce Training worth joining. Enroll for SalesForce CRM Integration Training Program and practice well. 

    ReplyDelete
  63. Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
    CRS Info Solutions Salesforce training for beginners

    ReplyDelete