什么情况下应该使用对象池(ObjectPool)

本文探讨了对象池模式的使用场景及优势,特别是在初始化成本高、实例化频率高且同时使用的实例数量较少的情况下。该模式有助于提升性能,尤其是在处理如数据库连接、套接字连接等昂贵资源时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

象池很多时候是很有用的模式,有时甚至是必须。但凡事过犹不及,对象池也只有在一些场景下才适合。

Object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instances in use at any one time is low. The pooled object is obtained in predictable time when creation of the new objects (especially over network) may take variable time.

However these benefits are mostly true for objects which are expensive with respect to time, such as database connections, socket connections, threads and large graphic objects like fonts or bitmaps. In certain situations, simple object pooling (which hold no external resources, but only occupy memory) may not be efficient and could decrease performance.

引用自:

http://en.wikipedia.org/wiki/Object_pool_pattern

References

http://www.ibm.com/developerworks/java/library/j-jtp09275/index.html


转自:http://vaporz.blog.51cto.com/3142258/706945

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值