Browser docs

Object Pool

Also known as

Resource Pool

Intent

When objects are expensive to create and they are needed only for short periods of time it is advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated objects tracking which ones are in use and which are available.

Explanation

Real world example

In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they are extremely expensive to create. The solution is to create a pool of them, track which ones are in-use, and instead of disposing them re-use the instances.

In plain words

Object Pool manages a set of instances instead of creating and destroying them on demand.

Wikipedia says

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a “pool” – rather than allocating and destroying them on demand.

Programmatic Example

Here’s the basic Oliphaunt class. These giants are very expensive to create.

 1public class Oliphaunt {
 2
 3  private static final AtomicInteger counter = new AtomicInteger(0);
 4
 5  private final int id;
 6
 7  public Oliphaunt() {
 8    id = counter.incrementAndGet();
 9    try {
10      Thread.sleep(1000);
11    } catch (InterruptedException e) {
12      e.printStackTrace();
13    }
14  }
15
16  public int getId() {
17    return id;
18  }
19
20  @Override
21  public String toString() {
22    return String.format("Oliphaunt id=%d", id);
23  }
24}

Next we present the ObjectPool and more specifically OliphauntPool.

 1public abstract class ObjectPool<T> {
 2
 3  private final Set<T> available = new HashSet<>();
 4  private final Set<T> inUse = new HashSet<>();
 5
 6  protected abstract T create();
 7
 8  public synchronized T checkOut() {
 9    if (available.isEmpty()) {
10      available.add(create());
11    }
12    var instance = available.iterator().next();
13    available.remove(instance);
14    inUse.add(instance);
15    return instance;
16  }
17
18  public synchronized void checkIn(T instance) {
19    inUse.remove(instance);
20    available.add(instance);
21  }
22
23  @Override
24  public synchronized String toString() {
25    return String.format("Pool available=%d inUse=%d", available.size(), inUse.size());
26  }
27}
28
29public class OliphauntPool extends ObjectPool<Oliphaunt> {
30
31  @Override
32  protected Oliphaunt create() {
33    return new Oliphaunt();
34  }
35}

Finally, here’s how we utilize the pool.

1    var pool = new OliphauntPool();
2    var oliphaunt1 = pool.checkOut();
3    var oliphaunt2 = pool.checkOut();
4    var oliphaunt3 = pool.checkOut();
5    pool.checkIn(oliphaunt1);
6    pool.checkIn(oliphaunt2);
7    var oliphaunt4 = pool.checkOut();
8    var oliphaunt5 = pool.checkOut();

Program output:

Pool available=0 inUse=0
Checked out Oliphaunt id=1
Pool available=0 inUse=1
Checked out Oliphaunt id=2
Checked out Oliphaunt id=3
Pool available=0 inUse=3
Checking in Oliphaunt id=1
Checking in Oliphaunt id=2
Pool available=2 inUse=1
Checked out Oliphaunt id=2
Checked out Oliphaunt id=1
Pool available=0 inUse=3

Class diagram

alt text

Applicability

Use the Object Pool pattern when

  • The objects are expensive to create (allocation cost).
  • You need a large number of short-lived objects (memory fragmentation).