Object Pooling in Java

后端 未结 7 1662
鱼传尺愫
鱼传尺愫 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:23

    Unless the object is expensive to create, I wouldn't bother.

    Benefits:

    • Fewer objects created - if object creation is expensive, this can be significant. (The canonical example is probably database connections, where "creation" includes making a network connection to the server, providing authentication etc.)

    Downsides:

    • More complicated code
    • Shared resource = locking; potential bottleneck
    • Violates GC's expectations of object lifetimes (most objects will be shortlived)

    Do you have an actual problem you're trying to solve, or is this speculative? I wouldn't think about doing something like this unless you've got benchmarks/profile runs showing that there's a problem.

    0 讨论(0)
  • 2020-12-29 08:31

    Object pools are generally only a good idea for expensive object like database connections. Up to Java 1.4.2, object pools could improve performance but as of Java 5.0 object pools where more likely to harm performance than help and often object pools were removed to improve performances (and simplicity)

    0 讨论(0)
  • 2020-12-29 08:38

    First law of optimization: don't do it. Second law: don't do it unless you actually have measured and know for a fact that you need to optimize and where.

    Only if objects are really expensive to create, and if they can actually be reused (you can reset the state with only public operations to something that can be reused) it can be effective.

    The two gains you mention are not really true: memory allocation in java is free (the cost was close to 10 cpu instructions, which is nothing). So reducing the creation of objects only saves you the time spent in the constructor. This can be a gain with really heavy objects that can be reused (database connections, threads) without changing: you reuse the same connection, the same thread.

    GC time is not reduced. In fact it can be worse. With moving generational GCs (Java is, or was up to 1.5) the cost of a GC run is determined by the number of alive objects, not by the released memory. Alive objects will be moved to another space in memory (this is what makes memory allocation so fast: free memory is contiguous inside each GC block) a couple of times before being marked as old and moved into the older generation memory space.

    Programming languages and support, as GC, were designed keeping in mind the common usage. If you steer away from the common usage in many cases you may end up with harder to read code that is less efficient.

    0 讨论(0)
  • 2020-12-29 08:42

    I agree with Jon Skeet's points, if you don't have a specific reason to create a pool of objects, I wouldn't bother.

    There are some situations when a pool is really helpful/necessary though. If you have a resource that is expensive to create, but can be reused (such as a database connection), it might make sense to use a pool. Also, in the case of database connections, a pool is useful for preventing your apps from opening too many concurrent connections to the database.

    0 讨论(0)
  • 2020-12-29 08:43

    Pooling will mean that you, typically, cannot make objects immutable. This leads to defencive copying so you ultimately wind up making many more copies than you would if you just made a new immutable object.

    Immutability is not always desirable, but more often than not you will find that things can be immutable. Making them not immutable so that you can reuse them in a pool is probably not a great idea.

    So, unless you know for certain that it is an issue don't bother. Make the code clear and easy to follow and odds are it will be fast enough. If it isn't then the fact that the code is clear and easy to follow will make it easier to speed it up (in general).

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题