This chapter will explain how Splitlog stores Message instances and will provide tips towards minimizing its memory consumption.
Any message in the ACCEPTED state (see Message Lifecycle will be stored inside Splitlog’s in-memory data store. Only messages stored here will be available for followers to retrieve.
Obviously, as the log files watched by Splitlog grow in size, so will the amount of memory that is required to store all those Message instances. To this end, Splitlog implements some smart techniques to help keep the memory consumption down. But it may need your cooperation.
The Java Virtual Machine supports two kinds of object references, weak and strong. Garbage collectors won’t remove objects from memory for as long as there are strong references to them. And Splitlog’s internals will only store Messages as weak references - as long as there’s nothing else pointing to those Message instances, they will be garbage-collected. So, what else can be pointing to those instances?
Keep in mind that Splitlog is very smart here. If you stop a particular follower, it will throw away all messages no longer accessible through any other follower. It will be impossible, from that point, to retrieve those messages in any way. When the strong references are gone, the messages are gone as well, for good.
There are some tips and tricks that you can leverage to manage Splitlog’s memory consumption.
All of these can be accomplished through LogWatchBuilder. But the most important thing you can do is this: write clean code! Properly limit the scope of your variables, don’t store what doesn’t need to be stored. The JVM will handle the rest for you.
Think you spotted a mistake in this document? A typo? Factual inaccuracy? Make an edit. Think of the legions of grateful users!