Designing a garbage-free application

In Which memory area does GC collect – stack or heap? section, I mentioned that a GC reclaims heap memorywhich can include (non-local) primitive data types or objects. In your Java application, heap memory can be used by these variables and objects from either of the following:

  • Third-party libraries used by your application
  • A JDK API
  • Your application classes

There are multiple ways to reduce garbage creationby preferring primitive data over objects, reusing buffers, using object pools, dumping temporary object creation, and others.

Here's proof that this is possible. One of the most popular garbage-free applications Log4j, which is a logging application by Apache, runs by default in a so-called garbage-free mode. This means that it reuses objects and buffers and avoids the allocation of temporary objects as much as possible. It also has a so-called low garbage mode; this mode is not entirely garbage free, but it also does not use ThreadLocal fields.  You can visit https://logging.apache.org/log4j/2.x/manual/garbagefree.html to learn more about it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.68.28