Skip to content

io7m-com/jpuddle

Repository files navigation

jpuddle

Maven Central Maven Central (snapshot) Codecov Java Version

com.io7m.jpuddle

JVM Platform Status
OpenJDK (Temurin) Current Linux Build (OpenJDK (Temurin) Current, Linux)
OpenJDK (Temurin) LTS Linux Build (OpenJDK (Temurin) LTS, Linux)
OpenJDK (Temurin) Current Windows Build (OpenJDK (Temurin) Current, Windows)
OpenJDK (Temurin) LTS Windows Build (OpenJDK (Temurin) LTS, Windows)

jpuddle

Java classes to pool heavyweight objects.

Features

Motivation

Object pooling is a programming technique where, instead of creating new objects to service requests, a small pool of objects is created and the objects within the pool are reused repeatedly to service requests instead. This was traditionally used by Java programs as a performance optimization in an attempt to reduce memory allocations and therefore reduce the amount of garbage collection that occurs. On modern Java virtual machines, however, object pooling as a means to improve performance in this manner is strongly contraindicated: Object allocations are extremely fast (on the order of a few tens of nanoseconds), escape analysis often eliminates allocations entirely, and modern garbage collectors are optimized to make short-lived objects essentially free.

With this in mind, it may not be clear why the com.io7m.jpuddle package should exist at all! The answer is that object pooling is still useful when the objects represent external resources that may be very expensive to acquire and/or the program should avoid acquiring too many of these resources at any given time. An example of this sort of use case is allocating short-lived framebuffer objects on a GPU. Graphics memory is typically in relatively short supply and creating an object on the GPU is generally considered to be an expensive and slow process (relative to simply allocating an object on the CPU side). A pool of framebuffer objects can be created that the application can reuse repeatedly without needing to create new objects, and the size of the pool can be bounded so that the application does not try to exceed the available GPU memory.

Usage

Implement the JPPoolableListenerType interface for the object you want to pool. The interface contains methods needed to estimate sizes for pool management, and methods to create and delete objects.

Then, create a pool with a soft limit of 100 objects and a hard limit of 200 objects:

JPPoolableListenerType<T> listener;

var p =
  JPPoolSynchronous.newPool(listener, 100L, 200L);

Use the get() method to retrieve objects from the pool.