Using COM+ object pooling with Delphi 6

Using COM+ object pooling with Delphi 6

By: Vincent Parrett

Abstract: Delphi 6 introduces support for COM+ object pooling, which can provide significant performance improvements under some circumstances. We take a look at Delphi 6s object pooling support. By Vincent Parrett.

<script type="text/javascript"></script>

Typically, when a client application instantiates a COM+ object, the COM+ runtime will create a new instance of the object. When your client is done with the object, COM+ will destroy the object. This all makes for very efficient use of memory resources, and under normal loads provides quite an acceptable performance.

However, under higher loads the creation and destruction of objects can become a substantial overhead. This is especially true if your COM+ objects are themselves allocating resources, instantiating other objects, and so on.

Object pooling can help in these situations by reducing the number of times an object must be instantiated or destroyed. When pooling is in effect, a client's request for an object will kick of a search to see if there is a ready-to-use object pool for objects of the appropriate type. If so, the COM+ system will provide and activate the object. If not, COM+ will create a new object. When the client is done with the object, COM+ will not destroy the object, but will return it to the object pool so it can be reused.

To get the best out of COM+, your objects should be stateless. To make use of object pooling, they must be stateless objects.

Object pooling with Delphi 6

Supporting object pooling in Delphi 6-generated COM+ objects is simple, however there are a few things that you need to take care with:

Threading model

The first is that you must select the Both threading model, otherwise object pooling will be disabled.



If you get this wrong, don't worry -- it's easy to fix. The New Transactional Object wizard generates code that looks something like this :

initialization
  TAutoObjectFactory.Create(ComServer, TMyCOMPlusObject, Class_MyCOMPlusObject,
    ciMultiInstance, 
   tmBoth); // make sure this is tmBoth.

IObjectControl.CanBePooled

COM+ objects must implement the IObjectControl interface. Delphi does this for us in the TMtsAutoObject. When a COM+ object is deactivated, COM+ calls the CanBePooled method on the IObjectControl interface. If this method returns true, the object is added to the object pool, otherwise it is destroyed. To make sure that this method returns true for out COM+ object, we need to set the Pooled property to True. A good place to do this is in the Initialize method, which we can easily override :

procedure TMyCOMPlusObject.Initialize;
begin
  inherited;
  Pooled := True;
end;

Turn on object pooling on the CoClass

Finally, the CoClass for our COM+ object has a setting on the COM+ tab in the Type Library Editor in which you can supposedly enable/disable object pooling and set the default time (in milliseconds) that an object will stay in the pool before being free. These settings seem to have no effect in my tests, but the Delphi Help says they must be checked.


How big is your pool?

Enabling pooling for Delphi COM+ objects is the easy part. Configuring the pool is somewhat more challenging. Why? Well, if you get it wrong, it could cause a serious performance degradation in your application.


The maximum pool size is the critical setting. New objects will be created if there are none available in the pool until the number of objects created equals the maximum pool setting. After this, requests for objects are queued. If no object becomes available within the creation timeout then the request will return with an error. Your client application should have error-handling to deal with this possibility.

The minimum pool size determines the minimum number of objects in the pool at any time. Objects will automatically be created when the application starts -- this setting is useful for preallocation of objects so they are immediately available to clients. Note that setting this to a high number can have performance implications, due to the amount of memory being allocated when the application starts up.

Changes to these settings do not take effect until the application has been restarted.

Stats 'n' stuff

Of course you would hope that by simply turning on object pooling, you will instantly see a performance improvement in your application. Often this will be difficult to quantify. (Did that page load faster than before? Maybe) The difference may be that the application scales somewhat better, or the application may actually perform worse than it did before. COM+ provides some stats that you can easily access which might give some idea of what's happening.

Enabling object statistics

In the details pane of the Component Services administrative tool, right-click the component that you want to configure, then click Properties. In the component properties dialog box, click the Activation tab. To enable object statistics for the component, select the Component supports events and statistics check box.

Monitoring object status

In the console tree of the Component Services administrative tool, open the folder for the application that includes the components you want to monitor. Right-click the Components folder, point to View, then click Status View. The details pane now displays the status view for all components in the application.

The table below describes the columns in the Status View :

ColumnDescription
ProgIDIdentifies a specific component.
ObjectsShows the number of objects that are currently held by client references.
ActivatedShows the number of objects that are currently activated.
PooledShows the total number of objects created by the pool, including objects that are in use and objects that are deactivated.
In CallIdentifies objects in call.
Call Time (ms)Shows the average call duration (in milliseconds) of method calls made in the last 20 seconds (up to 20 calls). Call time is measured at the object and does not include the time used to traverse the network.

The moment of truth

The best way to determine what (if anything) may be gained by using object pooling is to do proper performance testing with and without object pooling enabled. Then perform experiments with the minimum and maximum pool sizes to get the best out of your hardware. Monitor the memory and CPU usage of your server. The settings you eventually end up with will be a tradeoff between memory usage and CPU load.

While researching this article, I was hoping to find some document somewhere that would provide some hard and fast rules for configuring COM+ and object pooling. But I've had no luck so far.

Vincent lives and works in Canberra, Australia, where he specializes (well, he's from Australia, so he specialises) in developing components, developer tools and web applications using Delphi. He can be reached at vincent@vsoft-tech.com.au and at http://www.finalbuilder.com

内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值