1

I am currently working on a simple UDP server for a game. I have already implemented multithreading with the help of a ThreadPoolExecutor. When reading about how to maximize performance I stumbled across ObjectPooling. After some reading I have been left with 3 questions:

1) Will a server program be able to take advantage of an ObjectPool?

2) Which "Objects" should be pooled and which should not?

3) How to create a simple ObjectPool?

Maxwell
  • 133
  • 1
  • 7

3 Answers3

2

Will a server program be able to take advantage of an ObjectPool?

This doesn't depend on the type of application. The usefulness of object pooling depends on the use-case.

Which "Objects" should be pooled and which should not?

Usually, objects which are expensive (in memory or CPU terms) to create should be pooled (still opinable). Avoid pooling any other type, as pooling can get quite expensive just by itself.

How to create a simple ObjectPool?

Don't. It's a complex subject.
Instead, use something such as Apache Commons Pool.


And I'll add my own

Should I care about object pooling?

Not until you benchmarked performance without using it.
Pooling is a risky pattern, especially in multi-threaded environments. The pool implementation should provide the required synchronization, but still, adding your own is not bad either, if that object maintains a state.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
2

Here are answers to your questions,

1) Will a server program be able to take advantage of an ObjectPool?

Yes, any object can be pooled. Usually objectpools are used if object creation is heavy operation.

2) Which "Objects" should be pooled and which should not?

Any object can be pooled.

3) How to create a simple ObjectPool?

You can implement simple pool using apache commons pool. For more information, refer https://commons.apache.org/proper/commons-pool/examples.html

EDIT: FYI, we implemented objectpool using apache commons pool for connecting to CORBA clients. Connecting to CORBA and creating object was taking more time so we implemented CORBA client pool.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Santosh
  • 874
  • 11
  • 21
  • anyobject can be pooled, but as long as your object is being cached somewhere in your code, it does not make sense, unless your object is immutable. – Ankit Mar 27 '19 at 13:33
1

1) Will a server program be able to take advantage of an ObjectPool?

irrespective of type of application, it depends upon use case, specially when performance of your program is memory and CPU sensitive.

2) Which "Objects" should be pooled and which should not?

IMO, what scenarios make a pool for certain class objects useful:

  • when system needs only fixed or limited type (states) of objects at a time
  • when you need to impose an upper limit on maximum objects of certain class
  • when object creation is memory and / or cpu intensive and objects can be reused as is, or with minimal number of operations
  • when object itself is memory intensive and / or used in high frequency

in addition to above criteria, object must not be cached OR must be immutable

3) How to create a simple ObjectPool?

Its a complex topic altogether, but in essence you need to build an object creator (sort of Factory) which should decide whether to create new objects or return existing with needed change in their states.

Ankit
  • 6,554
  • 6
  • 49
  • 71