In Which memory area does GC collect – stack or heap? section, I mentioned that a GC reclaims heap memory—which 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 creation—by 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...