Object Pool and Lazy Loading: Make It Once, Share It, or Make It Late
When something slow to make is made for every request, or something big is made at the start and hardly used, the app slows down or runs out; an object pool makes a few and lends them out and back, and lazy loading makes a thing the first time it's needed, then keeps it.
Journeyman · OOP · Software Development · Updated
After this lesson you can
- Share a few expensive objects through a pool that lends them out and takes them back
- Delay making an expensive object until the first time it's needed, then keep it
- Spot the smells that point to a pool or lazy loading: something slow made again and again, or made early and rarely used
Key takeaways
- The smells: something slow to make (a database connection) made for every request, or something big made at the start and rarely used.
- An object pool keeps a few ready objects, lends one out, takes it back, and makes callers wait when all of them are out.
- Lazy loading makes an expensive object the first time someone asks for it, then keeps it for the next time.
- You'll mostly use these, not write them: database libraries pool connections, and Lazy<T> and cached_property load lazily.
Before this: Flyweight: Share What's the Same, and, for the second half, Proxy: The Stand-In at the Door.
On game day, thousands of fans open Harbor Stadium's ticket app and tap seats on the map to see their prices. Each tap asks the stadium's ticket database. Two things go wrong at kickoff. Every tap opens a brand-new connection to the database, which takes time (about 50 milliseconds each) and which the database allows only so many of at once. And the seat map loads a photo of the view from every one of its 40 sections before it shows anything, though most fans look at one or two. The last lesson shared things everyone can use at the same moment. These are different: a connection can serve only one lookup at a time, and a photo might never be needed at all. By the end of this lesson you'll have met two patterns for things that are expensive to make, the object pool and lazy loading.
The symptom: a new connection for every tap
Console.WriteLine("12 price lookups, one after another");
for (int fan = 1; fan <= 12; fan++)
{
LookUpPrice("Row 12, seat 7");
}
Console.WriteLine( quot;Connections opened: {Connection.Opened}");
Console.WriteLine("6 lookups at the same moment");
List<Connection> busy = new List<Connection>();
for (int fan = 1; fan <= 6; fan++)
{
busy.Add(new Connection());
}
Console.WriteLine( quot;Connections opened: {Connection.Opened}");
// Every lookup opens its own connection, uses it once and closes it.
int LookUpPrice(string seat)
{
Connection connection = new Connection();
int price = connection.PriceOf(seat);
connection.Close();
return price;
}
// Opening a connection to the ticket database is slow, and the database allows only so many at once.
class Connection
{
public static int Opened { get; private set; }
public Connection() { Opened++; }
public int PriceOf(string seat) => seat.StartsWith("Row 12") ? 90 : 40;
public void Close() { }
}
12 price lookups, one after another
Connections opened: 12
6 lookups at the same moment
Connections opened: 18
// Opening a connection to the ticket database is slow, and the database allows only so many at once.
static class Connection {
static int opened = 0;
Connection() { opened++; }
int priceOf(String seat) { return seat.startsWith("Row 12") ? 90 : 40; }
void close() { }
}
// Every lookup opens its own connection, uses it once and closes it.
int lookUpPrice(String seat) {
Connection connection = new Connection();
int price = connection.priceOf(seat);
connection.close();
return price;
}
void main() {
IO.println("12 price lookups, one after another");
for (int fan = 1; fan <= 12; fan++) {
lookUpPrice("Row 12, seat 7");
}
IO.println("Connections opened: " + Connection.opened);
IO.println("6 lookups at the same moment");
List<Connection> busy = new ArrayList<>();
for (int fan = 1; fan <= 6; fan++) {
busy.add(new Connection());
}
IO.println("Connections opened: " + Connection.opened);
}
12 price lookups, one after another
Connections opened: 12
6 lookups at the same moment
Connections opened: 18
class Connection:
"""Opening a connection to the ticket database is slow, and the database allows only so many at once."""
opened = 0
def __init__(self):
Connection.opened += 1
def price_of(self, seat):
return 90 if seat.startswith("Row 12") else 40
def close(self):
pass
def look_up_price(seat):
"""Every lookup opens its own connection, uses it once and closes it."""
connection = Connection()
price = connection.price_of(seat)
connection.close()
return price
print("12 price lookups, one after another")
for fan in range(12):
look_up_price("Row 12, seat 7")
print(f"Connections opened: {Connection.opened}")
print("6 lookups at the same moment")
busy = [Connection() for fan in range(6)]
print(f"Connections opened: {Connection.opened}")
12 price lookups, one after another
Connections opened: 12
6 lookups at the same moment
Connections opened: 18
// Opening a connection to the ticket database is slow, and the database allows only so many at once.
class Connection {
static opened = 0;
constructor() { Connection.opened++; }
priceOf(seat) { return seat.startsWith("Row 12") ? 90 : 40; }
close() { }
}
// Every lookup opens its own connection, uses it once and closes it.
function lookUpPrice(seat) {
const connection = new Connection();
const price = connection.priceOf(seat);
connection.close();
return price;
}
console.log("12 price lookups, one after another");
for (let fan = 1; fan <= 12; fan++) {
lookUpPrice("Row 12, seat 7");
}
console.log(`Connections opened: ${Connection.opened}`);
console.log("6 lookups at the same moment");
const busy = [];
for (let fan = 1; fan <= 6; fan++) {
busy.push(new Connection());
}
console.log(`Connections opened: ${Connection.opened}`);
12 price lookups, one after another
Connections opened: 12
6 lookups at the same moment
Connections opened: 18
// Opening a connection to the ticket database is slow, and the database allows only so many at once.
class Connection {
static opened = 0;
constructor() { Connection.opened++; }
priceOf(seat: string): number { return seat.startsWith("Row 12") ? 90 : 40; }
close(): void { }
}
// Every lookup opens its own connection, uses it once and closes it.
function lookUpPrice(seat: string): number {
const connection = new Connection();
const price = connection.priceOf(seat);
connection.close();
return price;
}
console.log("12 price lookups, one after another");
for (let fan = 1; fan <= 12; fan++) {
lookUpPrice("Row 12, seat 7");
}
console.log(`Connections opened: ${Connection.opened}`);
console.log("6 lookups at the same moment");
const busy: Connection[] = [];
for (let fan = 1; fan <= 6; fan++) {
busy.push(new Connection());
}
console.log(`Connections opened: ${Connection.opened}`);
12 price lookups, one after another
Connections opened: 12
6 lookups at the same moment
Connections opened: 18
Twelve fans, one after another, and twelve connections, each opened, used once and thrown away. Then six fans tap at the same moment, and six more open. At kickoff it's two thousand fans, and the database starts turning connections away.
Read it as a doctor would:
- The symptom: something slow to make is made for every request and thrown away, and nothing limits how many there are at once.
- The cause: each lookup makes its own connection, though one connection could serve lookup after lookup.
- The cure: coming up, once we've tried the obvious fixes.
What would you try?
One shared connection for the whole app, a Singleton. No more opening, but a connection does one lookup at a time, so at kickoff two thousand fans queue behind it.
Keep every connection open forever. No reopening, but two thousand fans means two thousand open connections, far past what the database allows.
Make opening faster. The time goes to the network and the database checking who you are, not to your code.
What we want: a few connections, kept open, each lent to one lookup at a time and given back afterward, with a limit, so a rush waits a moment instead of overwhelming the database.
The everyday version: a bike-share rack
A city's bike-share rack holds a few bikes. You take one, ride it, and put it back for the next person. The city doesn't build a new bike for every trip or throw it away after one, and when the rack is empty, you wait a few minutes for someone to bring one back.

The fix: a pool lends them out and takes them back
The fix in one sentence: keep a small set of ready objects; lend one to each caller, take it back when they're done, make a new one only while under the limit, and make callers wait when every one is out.

ConnectionPool pool = new ConnectionPool(5);
Console.WriteLine("12 price lookups, one after another");
for (int fan = 1; fan <= 12; fan++)
{
LookUpPrice("Row 12, seat 7");
}
Console.WriteLine( quot;Connections opened: {Connection.Opened}");
Console.WriteLine("6 lookups at the same moment");
List<Connection> busy = new List<Connection>();
int waiting = 0;
for (int fan = 1; fan <= 6; fan++)
{
Connection? connection = pool.Borrow();
if (connection == null) { waiting++; } else { busy.Add(connection); }
}
Console.WriteLine( quot;Connections opened: {Connection.Opened}");
Console.WriteLine( quot;{waiting} lookup waits for a free connection");
busy.ForEach(pool.GiveBack);
int LookUpPrice(string seat)
{
Connection connection = pool.Borrow()!;
int price = connection.PriceOf(seat);
pool.GiveBack(connection);
return price;
}
// Hands out a few connections and takes them back, opening a new one only when none is free.
class ConnectionPool(int size)
{
private readonly List<Connection> free = new List<Connection>();
private int lent = 0;
public Connection? Borrow()
{
if (lent == size) { return null; }
lent++;
if (free.Count > 0)
{
Connection reused = free[^1];
free.RemoveAt(free.Count - 1);
return reused;
}
return new Connection();
}
public void GiveBack(Connection connection)
{
lent--;
free.Add(connection);
}
}
class Connection
{
public static int Opened { get; private set; }
public Connection() { Opened++; }
public int PriceOf(string seat) => seat.StartsWith("Row 12") ? 90 : 40;
}
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
static class Connection {
static int opened = 0;
Connection() { opened++; }
int priceOf(String seat) { return seat.startsWith("Row 12") ? 90 : 40; }
}
// Hands out a few connections and takes them back, opening a new one only when none is free.
static class ConnectionPool {
private final int size;
private final List<Connection> free = new ArrayList<>();
private int lent = 0;
ConnectionPool(int size) { this.size = size; }
Connection borrow() {
if (lent == size) { return null; }
lent++;
return free.isEmpty() ? new Connection() : free.removeLast();
}
void giveBack(Connection connection) {
lent--;
free.add(connection);
}
}
ConnectionPool pool = new ConnectionPool(5);
int lookUpPrice(String seat) {
Connection connection = pool.borrow();
int price = connection.priceOf(seat);
pool.giveBack(connection);
return price;
}
void main() {
IO.println("12 price lookups, one after another");
for (int fan = 1; fan <= 12; fan++) {
lookUpPrice("Row 12, seat 7");
}
IO.println("Connections opened: " + Connection.opened);
IO.println("6 lookups at the same moment");
List<Connection> busy = new ArrayList<>();
int waiting = 0;
for (int fan = 1; fan <= 6; fan++) {
Connection connection = pool.borrow();
if (connection == null) { waiting++; } else { busy.add(connection); }
}
IO.println("Connections opened: " + Connection.opened);
IO.println(waiting + " lookup waits for a free connection");
busy.forEach(pool::giveBack);
}
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
class Connection:
opened = 0
def __init__(self):
Connection.opened += 1
def price_of(self, seat):
return 90 if seat.startswith("Row 12") else 40
class ConnectionPool:
"""Hands out a few connections and takes them back, opening a new one only when none is free."""
def __init__(self, size):
self.size = size
self.free = []
self.lent = 0
def borrow(self):
if self.lent == self.size:
return None
self.lent += 1
return self.free.pop() if self.free else Connection()
def give_back(self, connection):
self.lent -= 1
self.free.append(connection)
pool = ConnectionPool(5)
def look_up_price(seat):
connection = pool.borrow()
price = connection.price_of(seat)
pool.give_back(connection)
return price
print("12 price lookups, one after another")
for fan in range(12):
look_up_price("Row 12, seat 7")
print(f"Connections opened: {Connection.opened}")
print("6 lookups at the same moment")
busy = []
waiting = 0
for fan in range(6):
connection = pool.borrow()
if connection is None:
waiting += 1
else:
busy.append(connection)
print(f"Connections opened: {Connection.opened}")
print(f"{waiting} lookup waits for a free connection")
for connection in busy:
pool.give_back(connection)
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
class Connection {
static opened = 0;
constructor() { Connection.opened++; }
priceOf(seat) { return seat.startsWith("Row 12") ? 90 : 40; }
}
// Hands out a few connections and takes them back, opening a new one only when none is free.
class ConnectionPool {
constructor(size) {
this.size = size;
this.free = [];
this.lent = 0;
}
borrow() {
if (this.lent === this.size) { return null; }
this.lent++;
return this.free.length > 0 ? this.free.pop() : new Connection();
}
giveBack(connection) {
this.lent--;
this.free.push(connection);
}
}
const pool = new ConnectionPool(5);
function lookUpPrice(seat) {
const connection = pool.borrow();
const price = connection.priceOf(seat);
pool.giveBack(connection);
return price;
}
console.log("12 price lookups, one after another");
for (let fan = 1; fan <= 12; fan++) {
lookUpPrice("Row 12, seat 7");
}
console.log(`Connections opened: ${Connection.opened}`);
console.log("6 lookups at the same moment");
const busy = [];
let waiting = 0;
for (let fan = 1; fan <= 6; fan++) {
const connection = pool.borrow();
if (connection === null) { waiting++; } else { busy.push(connection); }
}
console.log(`Connections opened: ${Connection.opened}`);
console.log(`${waiting} lookup waits for a free connection`);
busy.forEach((connection) => pool.giveBack(connection));
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
class Connection {
static opened = 0;
constructor() { Connection.opened++; }
priceOf(seat: string): number { return seat.startsWith("Row 12") ? 90 : 40; }
}
// Hands out a few connections and takes them back, opening a new one only when none is free.
class ConnectionPool {
size: number;
free: Connection[] = [];
lent = 0;
constructor(size: number) { this.size = size; }
borrow(): Connection | null {
if (this.lent === this.size) { return null; }
this.lent++;
return this.free.pop() ?? new Connection();
}
giveBack(connection: Connection): void {
this.lent--;
this.free.push(connection);
}
}
const pool = new ConnectionPool(5);
function lookUpPrice(seat: string): number {
const connection = pool.borrow()!;
const price = connection.priceOf(seat);
pool.giveBack(connection);
return price;
}
console.log("12 price lookups, one after another");
for (let fan = 1; fan <= 12; fan++) {
lookUpPrice("Row 12, seat 7");
}
console.log(`Connections opened: ${Connection.opened}`);
console.log("6 lookups at the same moment");
const busy: Connection[] = [];
let waiting = 0;
for (let fan = 1; fan <= 6; fan++) {
const connection = pool.borrow();
if (connection === null) { waiting++; } else { busy.push(connection); }
}
console.log(`Connections opened: ${Connection.opened}`);
console.log(`${waiting} lookup waits for a free connection`);
busy.forEach((connection) => pool.giveBack(connection));
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
One connection served all twelve lookups, one after another. In the rush, the pool opened up to its limit of five, and the sixth fan waits a moment for one to come back, instead of the database turning someone away. A pool of database connections like this one has its own name, a connection pool.
The catch: a caller that forgets to give a connection back, or hits an error before it can, keeps it forever. After five of those, every fan waits. That's called a leak.
The modern version: giving it back by itself
Every language has a way to run "give it back" however a block of code ends, even when it ends with an error:
ConnectionPool pool = new ConnectionPool(5);
Console.WriteLine("12 price lookups, one after another");
for (int fan = 1; fan <= 11; fan++)
{
LookUpPrice("Row 12, seat 7");
}
// The 12th fan asks for a seat that doesn't exist. The lookup fails, and the connection still goes back.
try { LookUpPrice("Gate B"); } catch (ArgumentException) { }
Console.WriteLine( quot;Connections opened: {Connection.Opened}");
Console.WriteLine("6 lookups at the same moment");
List<Lease> busy = new List<Lease>();
int waiting = 0;
for (int fan = 1; fan <= 6; fan++)
{
Lease? lease = pool.Lend();
if (lease == null) { waiting++; } else { busy.Add(lease); }
}
Console.WriteLine( quot;Connections opened: {Connection.Opened}");
Console.WriteLine( quot;{waiting} lookup waits for a free connection");
busy.ForEach(lease => lease.Dispose());
int LookUpPrice(string seat)
{
// "using" hands the connection back at the end of the method, even when PriceOf throws.
using Lease lease = pool.Lend()!;
return lease.Connection.PriceOf(seat);
}
class ConnectionPool(int size)
{
private readonly Stack<Connection> free = new Stack<Connection>();
private int lent = 0;
public Lease? Lend()
{
if (lent == size) { return null; }
lent++;
return new Lease(free.Count > 0 ? free.Pop() : new Connection(), this);
}
public void GiveBack(Connection connection)
{
lent--;
free.Push(connection);
}
}
// A borrowed connection that gives itself back when it's disposed.
class Lease(Connection connection, ConnectionPool pool) : IDisposable
{
public Connection Connection => connection;
public void Dispose() => pool.GiveBack(connection);
}
class Connection
{
public static int Opened { get; private set; }
public Connection() { Opened++; }
public int PriceOf(string seat) => seat.StartsWith("Row 12") ? 90 : seat.StartsWith("Row") ? 40 : throw new ArgumentException("No such seat: " + seat);
}
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
static class Connection {
static int opened = 0;
Connection() { opened++; }
int priceOf(String seat) {
if (!seat.startsWith("Row")) { throw new IllegalArgumentException("No such seat: " + seat); }
return seat.startsWith("Row 12") ? 90 : 40;
}
}
// A borrowed connection that gives itself back when it's closed.
record Lease(Connection connection, ConnectionPool pool) implements AutoCloseable {
public void close() { pool.giveBack(connection); }
}
static class ConnectionPool {
private final int size;
private final List<Connection> free = new ArrayList<>();
private int lent = 0;
ConnectionPool(int size) { this.size = size; }
Lease lend() {
if (lent == size) { return null; }
lent++;
return new Lease(free.isEmpty() ? new Connection() : free.removeLast(), this);
}
void giveBack(Connection connection) {
lent--;
free.add(connection);
}
}
ConnectionPool pool = new ConnectionPool(5);
int lookUpPrice(String seat) {
// try-with-resources hands the connection back at the end, even when priceOf throws.
try (Lease lease = pool.lend()) {
return lease.connection().priceOf(seat);
}
}
void main() {
IO.println("12 price lookups, one after another");
for (int fan = 1; fan <= 11; fan++) {
lookUpPrice("Row 12, seat 7");
}
// The 12th fan asks for a seat that doesn't exist. The lookup fails, and the connection still goes back.
try { lookUpPrice("Gate B"); } catch (IllegalArgumentException error) { }
IO.println("Connections opened: " + Connection.opened);
IO.println("6 lookups at the same moment");
List<Lease> busy = new ArrayList<>();
int waiting = 0;
for (int fan = 1; fan <= 6; fan++) {
Lease lease = pool.lend();
if (lease == null) { waiting++; } else { busy.add(lease); }
}
IO.println("Connections opened: " + Connection.opened);
IO.println(waiting + " lookup waits for a free connection");
busy.forEach(Lease::close);
}
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
from contextlib import contextmanager
class Connection:
opened = 0
def __init__(self):
Connection.opened += 1
def price_of(self, seat):
if not seat.startswith("Row"):
raise ValueError(f"No such seat: {seat}")
return 90 if seat.startswith("Row 12") else 40
class ConnectionPool:
def __init__(self, size):
self.size = size
self.free = []
self.lent = 0
def borrow(self):
if self.lent == self.size:
return None
self.lent += 1
return self.free.pop() if self.free else Connection()
def give_back(self, connection):
self.lent -= 1
self.free.append(connection)
@contextmanager
def connection(self):
"""Lends a connection for one "with" block, and gives it back at the end, even after an error."""
connection = self.borrow()
try:
yield connection
finally:
self.give_back(connection)
pool = ConnectionPool(5)
def look_up_price(seat):
with pool.connection() as connection:
return connection.price_of(seat)
print("12 price lookups, one after another")
for fan in range(11):
look_up_price("Row 12, seat 7")
# The 12th fan asks for a seat that doesn't exist. The lookup fails, and the connection still goes back.
try:
look_up_price("Gate B")
except ValueError:
pass
print(f"Connections opened: {Connection.opened}")
print("6 lookups at the same moment")
busy = []
waiting = 0
for fan in range(6):
connection = pool.borrow()
if connection is None:
waiting += 1
else:
busy.append(connection)
print(f"Connections opened: {Connection.opened}")
print(f"{waiting} lookup waits for a free connection")
for connection in busy:
pool.give_back(connection)
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
class Connection {
static opened = 0;
constructor() { Connection.opened++; }
priceOf(seat) {
if (!seat.startsWith("Row")) { throw new Error(`No such seat: ${seat}`); }
return seat.startsWith("Row 12") ? 90 : 40;
}
}
class ConnectionPool {
constructor(size) {
this.size = size;
this.free = [];
this.lent = 0;
}
borrow() {
if (this.lent === this.size) { return null; }
this.lent++;
return this.free.length > 0 ? this.free.pop() : new Connection();
}
giveBack(connection) {
this.lent--;
this.free.push(connection);
}
// Lends a connection to one function, and gives it back when the function ends, even after an error.
use(work) {
const connection = this.borrow();
try {
return work(connection);
} finally {
this.giveBack(connection);
}
}
}
const pool = new ConnectionPool(5);
function lookUpPrice(seat) {
return pool.use((connection) => connection.priceOf(seat));
}
console.log("12 price lookups, one after another");
for (let fan = 1; fan <= 11; fan++) {
lookUpPrice("Row 12, seat 7");
}
// The 12th fan asks for a seat that doesn't exist. The lookup fails, and the connection still goes back.
try { lookUpPrice("Gate B"); } catch { }
console.log(`Connections opened: ${Connection.opened}`);
console.log("6 lookups at the same moment");
const busy = [];
let waiting = 0;
for (let fan = 1; fan <= 6; fan++) {
const connection = pool.borrow();
if (connection === null) { waiting++; } else { busy.push(connection); }
}
console.log(`Connections opened: ${Connection.opened}`);
console.log(`${waiting} lookup waits for a free connection`);
busy.forEach((connection) => pool.giveBack(connection));
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
class Connection {
static opened = 0;
constructor() { Connection.opened++; }
priceOf(seat: string): number {
if (!seat.startsWith("Row")) { throw new Error(`No such seat: ${seat}`); }
return seat.startsWith("Row 12") ? 90 : 40;
}
}
class ConnectionPool {
size: number;
free: Connection[] = [];
lent = 0;
constructor(size: number) { this.size = size; }
borrow(): Connection | null {
if (this.lent === this.size) { return null; }
this.lent++;
return this.free.pop() ?? new Connection();
}
giveBack(connection: Connection): void {
this.lent--;
this.free.push(connection);
}
// Lends a connection to one function, and gives it back when the function ends, even after an error.
use<T>(work: (connection: Connection) => T): T {
const connection = this.borrow()!;
try {
return work(connection);
} finally {
this.giveBack(connection);
}
}
}
const pool = new ConnectionPool(5);
function lookUpPrice(seat: string): number {
return pool.use((connection) => connection.priceOf(seat));
}
console.log("12 price lookups, one after another");
for (let fan = 1; fan <= 11; fan++) {
lookUpPrice("Row 12, seat 7");
}
// The 12th fan asks for a seat that doesn't exist. The lookup fails, and the connection still goes back.
try { lookUpPrice("Gate B"); } catch { }
console.log(`Connections opened: ${Connection.opened}`);
console.log("6 lookups at the same moment");
const busy: Connection[] = [];
let waiting = 0;
for (let fan = 1; fan <= 6; fan++) {
const connection = pool.borrow();
if (connection === null) { waiting++; } else { busy.push(connection); }
}
console.log(`Connections opened: ${Connection.opened}`);
console.log(`${waiting} lookup waits for a free connection`);
busy.forEach((connection) => pool.giveBack(connection));
12 price lookups, one after another
Connections opened: 1
6 lookups at the same moment
Connections opened: 5
1 lookup waits for a free connection
The twelfth fan asked for a seat that doesn't exist, so that lookup failed, and its connection still went back: the counts are the same as before. C# does it with using and IDisposable, Java with try-with-resources and AutoCloseable, and Python with with and a context manager. JavaScript and TypeScript lend the connection to a function and give it back in a finally.
How it got its name
A pool is a shared supply that people draw from and return to, like a company's pool of cars. The object pool isn't in the 1994 book; working teams named it, mostly for database connections and threads, because they kept building the same lender. Today you'll rarely write one: you'll use one and remember to give back.
Where you've already used it
- Database libraries pool connections for you. In C#,
new SqlConnection(...)withusingborrows from a pool and gives back at the end; Java apps use pools such as HikariCP, Python's SQLAlchemy keeps one, and node-postgres has aPool. Closing a pooled connection hands it back instead of closing it. - Thread pools: .NET's
ThreadPooland Java'sExecutorServicekeep a few worker threads and hand them jobs. - HTTP clients keep a pool of open network connections to the servers they call often.
Make it late: lazy loading
Now the photos. Here is the map as it starts today:
// The map has 40 sections, numbered 101 to 140, and each has a photo of the view from its seats.
List<SectionView> views = Enumerable.Range(101, 40).Select(section => new SectionView(section)).ToList();
Console.WriteLine( quot;Views loaded: {SectionView.Loaded} of {views.Count}");
Console.WriteLine("Seat map ready");
Console.WriteLine(views.First(view => view.Section == 112).Photo);
class SectionView
{
public static int Loaded { get; private set; }
public SectionView(int section)
{
Section = section;
// Loading a photo is slow (about 2 MB each), and it happens for every section, at the start.
Photo = quot;the view from section {section}";
Loaded++;
}
public int Section { get; }
public string Photo { get; }
}
Views loaded: 40 of 40
Seat map ready
the view from section 112
static class SectionView {
static int loaded = 0;
final int section;
final String photo;
SectionView(int section) {
this.section = section;
// Loading a photo is slow (about 2 MB each), and it happens for every section, at the start.
this.photo = "the view from section " + section;
loaded++;
}
}
void main() {
// The map has 40 sections, numbered 101 to 140, and each has a photo of the view from its seats.
List<SectionView> views = new ArrayList<>();
for (int section = 101; section <= 140; section++) {
views.add(new SectionView(section));
}
IO.println("Views loaded: " + SectionView.loaded + " of " + views.size());
IO.println("Seat map ready");
IO.println(views.stream().filter(view -> view.section == 112).findFirst().get().photo);
}
Views loaded: 40 of 40
Seat map ready
the view from section 112
class SectionView:
loaded = 0
def __init__(self, section):
self.section = section
# Loading a photo is slow (about 2 MB each), and it happens for every section, at the start.
self.photo = f"the view from section {section}"
SectionView.loaded += 1
# The map has 40 sections, numbered 101 to 140, and each has a photo of the view from its seats.
views = {section: SectionView(section) for section in range(101, 141)}
print(f"Views loaded: {SectionView.loaded} of {len(views)}")
print("Seat map ready")
print(views[112].photo)
Views loaded: 40 of 40
Seat map ready
the view from section 112
class SectionView {
static loaded = 0;
constructor(section) {
this.section = section;
// Loading a photo is slow (about 2 MB each), and it happens for every section, at the start.
this.photo = `the view from section ${section}`;
SectionView.loaded++;
}
}
// The map has 40 sections, numbered 101 to 140, and each has a photo of the view from its seats.
const views = new Map();
for (let section = 101; section <= 140; section++) {
views.set(section, new SectionView(section));
}
console.log(`Views loaded: ${SectionView.loaded} of ${views.size}`);
console.log("Seat map ready");
console.log(views.get(112).photo);
Views loaded: 40 of 40
Seat map ready
the view from section 112
class SectionView {
static loaded = 0;
section: number;
photo: string;
constructor(section: number) {
this.section = section;
// Loading a photo is slow (about 2 MB each), and it happens for every section, at the start.
this.photo = `the view from section ${section}`;
SectionView.loaded++;
}
}
// The map has 40 sections, numbered 101 to 140, and each has a photo of the view from its seats.
const views = new Map<number, SectionView>();
for (let section = 101; section <= 140; section++) {
views.set(section, new SectionView(section));
}
console.log(`Views loaded: ${SectionView.loaded} of ${views.size}`);
console.log("Seat map ready");
console.log(views.get(112)!.photo);
Views loaded: 40 of 40
Seat map ready
the view from section 112
All 40 photos load before the map says "Seat map ready", about 80 MB, and most fans will look at one or two. The symptom: something big made at the start, used rarely or never, so starting up is slow. The cause: each photo is made when its section is made, not when someone wants to see it.
A smaller photo would still load 40 of them, and loading them all later, after the map appears, still costs the memory. What we want: load a photo the first time someone asks for it, and keep it for the next time.
A café does this with decaf: it brews a pot the first time someone orders one, and keeps it warm for the next person, instead of brewing every kind of coffee at opening time.
The fix in one sentence: leave an expensive object unmade until the first time it's asked for, then make it, keep it, and hand out the same one from then on. That's lazy loading.

List<SectionView> views = Enumerable.Range(101, 40).Select(section => new SectionView(section)).ToList();
Console.WriteLine("Seat map ready");
SectionView picked = views.First(view => view.Section == 112);
Console.WriteLine(picked.Photo);
Console.WriteLine(picked.Photo);
Console.WriteLine( quot;Views loaded: {SectionView.Loaded} of {views.Count}");
class SectionView
{
public static int Loaded { get; private set; }
// Lazy<T> runs the function the first time .Value is read, then keeps the answer.
private readonly Lazy<string> photo;
public SectionView(int section)
{
Section = section;
photo = new Lazy<string>(() =>
{
Console.WriteLine( quot;Loading the view from section {section}");
Loaded++;
return quot;the view from section {section}";
});
}
public int Section { get; }
public string Photo => photo.Value;
}
Seat map ready
Loading the view from section 112
the view from section 112
the view from section 112
Views loaded: 1 of 40
static class SectionView {
static int loaded = 0;
final int section;
private String photo = null;
SectionView(int section) { this.section = section; }
// Java has no built-in lazy field: load the photo the first time it's asked for, then keep it.
String photo() {
if (photo == null) {
IO.println("Loading the view from section " + section);
loaded++;
photo = "the view from section " + section;
}
return photo;
}
}
void main() {
List<SectionView> views = new ArrayList<>();
for (int section = 101; section <= 140; section++) {
views.add(new SectionView(section));
}
IO.println("Seat map ready");
SectionView picked = views.stream().filter(view -> view.section == 112).findFirst().get();
IO.println(picked.photo());
IO.println(picked.photo());
IO.println("Views loaded: " + SectionView.loaded + " of " + views.size());
}
Seat map ready
Loading the view from section 112
the view from section 112
the view from section 112
Views loaded: 1 of 40
from functools import cached_property
class SectionView:
loaded = 0
def __init__(self, section):
self.section = section
@cached_property
def photo(self):
"""Runs the first time photo is read, then keeps the answer."""
print(f"Loading the view from section {self.section}")
SectionView.loaded += 1
return f"the view from section {self.section}"
views = {section: SectionView(section) for section in range(101, 141)}
print("Seat map ready")
print(views[112].photo)
print(views[112].photo)
print(f"Views loaded: {SectionView.loaded} of {len(views)}")
Seat map ready
Loading the view from section 112
the view from section 112
the view from section 112
Views loaded: 1 of 40
class SectionView {
static loaded = 0;
#photo = null;
constructor(section) { this.section = section; }
// A getter that loads the photo the first time it's read, then keeps it.
get photo() {
if (this.#photo === null) {
console.log(`Loading the view from section ${this.section}`);
SectionView.loaded++;
this.#photo = `the view from section ${this.section}`;
}
return this.#photo;
}
}
const views = new Map();
for (let section = 101; section <= 140; section++) {
views.set(section, new SectionView(section));
}
console.log("Seat map ready");
console.log(views.get(112).photo);
console.log(views.get(112).photo);
console.log(`Views loaded: ${SectionView.loaded} of ${views.size}`);
Seat map ready
Loading the view from section 112
the view from section 112
the view from section 112
Views loaded: 1 of 40
class SectionView {
static loaded = 0;
section: number;
#photo: string | null = null;
constructor(section: number) { this.section = section; }
// A getter that loads the photo the first time it's read, then keeps it.
get photo(): string {
if (this.#photo === null) {
console.log(`Loading the view from section ${this.section}`);
SectionView.loaded++;
this.#photo = `the view from section ${this.section}`;
}
return this.#photo;
}
}
const views = new Map<number, SectionView>();
for (let section = 101; section <= 140; section++) {
views.set(section, new SectionView(section));
}
console.log("Seat map ready");
console.log(views.get(112)!.photo);
console.log(views.get(112)!.photo);
console.log(`Views loaded: ${SectionView.loaded} of ${views.size}`);
Seat map ready
Loading the view from section 112
the view from section 112
the view from section 112
Views loaded: 1 of 40
The map is ready at once, the photo loads the first time it's asked for, and the second time it's already there. C#'s Lazy<T> and Python's cached_property do the "first time only" part for you; JavaScript and TypeScript use a getter that fills a private field; Java has no built-in lazy field, so it checks for null itself.
You've seen this before. In Proxy: The Stand-In at the Door, a small thumbnail stood in for a full photo and loaded the full one only when tapped: that stand-in was lazy loading, behind a proxy.
When not to use them
- A pool, when objects are cheap to make. Pooling small objects costs more bookkeeping than it saves; today's languages clean up short-lived objects well.
- A pool, when your library already has one, which almost every database library does.
- A pool, for objects that keep what the last user did (a connection left halfway through a change) unless the pool resets each one before lending it again.
- Lazy loading, when the thing is needed almost every time. Then load it up front, so the first user doesn't wait at a bad moment, like a fan at the gate at kickoff.
- Lazy loading in code with many threads, unless the lazy helper is safe for them:
Lazy<T>is by default, and a hand-writtennullcheck isn't.
How often you'll meet them: object pools every day, written rarely (our estimate: every database connection you open is pooled); lazy loading often, in frameworks, image-heavy pages and data loaded from a database.
Check yourself
2 questions to try on the lesson page.
So what
Not everything should be made fresh every time. Make it once and share it with everyone (Flyweight), make a few and lend them out (Object Pool), or make it the first time it's needed (Lazy Loading), whichever matches how the thing is used.