Object Pooling in Java

后端 未结 7 1701
鱼传尺愫
鱼传尺愫 2020-12-29 08:19

What are the pro\'s and con\'s of maintaining a pool of frequently used objects and grab one from the pool instead of creating a new one. Something like string interning exc

7条回答
  •  鱼传尺愫
    2020-12-29 08:43

    Don't.

    This is 2001 thinking. The only object "pool" that is still worth anything now a days is a singleton. I use singletons only to reduce the object creation for purposes of profiling (so I can see more clearly what is impacting the code).

    Anything else you are just fragmenting memory for no good purpose.

    Go ahead and run a profile on creating a 1,000,000 objects. It is insignificant.

    Old article here.

提交回复
热议问题