Circular Buffer using arrays in java


August 7, 2014 . Comments
Tags: circular buffer, java

 
 
  

I have seen many implementations online for implementing Circular Buffer but haven't found a clean one. To me this is a crucial functionality and has a lot of its uses in software. Hence here goes my code.

Now you had seen the code here you can see where this can be used. The most commonly used place is logging where the system wants to allocate only certain amount of memory or maintain one hour worth of log data. If your system supports 1000,000 log entries per minute then you would want your array size to support 60 mins worth of data to be 60,000,000. If we don't use a fixed buffer that can be used continuously for a short span of time we would be putting lot of stress on JVM to allocate and free memory. You will be seeing me using this approach in coding for various other types through the blog.

60,000,000 million sounds like a lot and it is for a small and average system. It is a good idea to preallocate the sizes than to incur the cost of resizing arrays on the fly makes planning memory and H/W for high performant systems easier and less stress on garbage collector.

If you like my blog drop a line and I am always happy to learn and share more.