This article is a mirror article of machine translation, please click here to jump to the original article.

View: 30226|Reply: 1

[.NET Core] Detailed explanation of Object Pool in .NET Core

[Copy link]
Posted on 2020-4-15 17:38:34 | | | |
Microsoft.Extensions.ObjectPool is part of the ASP.NET Core infrastructure that supports keeping a set of objects in memory for reuse instead of allowing garbage collection of objects.
If the objects you want to manage are, you may need to use object pools:

  • Allocation/initialization is costly.
  • Represents some limited resource.
  • Use predictably and frequently.


Reuse is an important topic and an inevitable problem that we often encounter in our daily development.

To take the simplest and most familiar example, database connection pools are reused database connections.

So what is the point of reuse?

Simply put, it reduces unnecessary resource loss.

In addition to database connections, there may be many other objects that need to be reused under different scenarios or requirements, and there is a so-called Object Pool.

You should have implemented similar functions yourself, either with ConcurrentBag, or ConcurrentQueue, or with other solutions.

This also shares an implementation in the Microsoft documentation

How to: Create an Object Pool by Using a ConcurrentBag

The hyperlink login is visible.

Of course, in .NET Core, Microsoft has helped us implement a simple Object Pool.

First, create a new .NET Core console project and use the nuget command to add the following package:

All the code is as follows:

Usage 1



Before creating a pool, we need to define a Policy. Here we directly use the included DefaultPooledObjectPolicy to construct it.

The object pool will have a maximum number of threads maintained.

Use the Get method of the pool object to take an object out of the object pool.

The above code runs the result

#1-464d2e03-604d-4451-b68a-8a3a2abdfccc-itsvse.com-1/1/0001 12:00:00 AM
#2-70122aa0-a949-4c63-b878-321efe64c234-itsvse.com-1/1/0001 12:00:00 AM
#3-de717815-796a-4349-a571-047acc125104-itsvse.com-1/1/0001 12:00:00 AM
#4-3a404341-a560-47f7-a3b0-0d477a8ae17f-itsvse.com-1/1/0001 12:00:00 AM
#5-51c96126-d424-4b58-b07c-6408e6c4cea6-itsvse.com-1/1/0001 12:00:00 AM
#6-7ea4d596-fd2a-43b3-959a-9e48da58a758-itsvse.com-1/1/0001 12:00:00 AM
#7-6874c64b-532d-4f92-a4fb-ff472da574a1-itsvse.com-1/1/0001 12:00:00 AM
#8-ca767674-2c07-4f26-975f-4711a31d795d-itsvse.com-1/1/0001 12:00:00 AM
#9-a9cd1859-a919-46a0-ae5d-85b6d3d11ccb-itsvse.com-1/1/0001 12:00:00 AM
#10-fe89ed8b-4dfd-4eef-b876-b2a60ee50958-itsvse.com-1/1/0001 12:00:00 AM
This result shows that the objects in the Object Pool are directly newed, and some properties are not devalued, which often does not have much practical meaning at this time.

Because DefaultPooledObjectPolicy is a new object directly, many times, this is not what we expect!

If you want to meet our actual use, you have to define a policy yourself!

Let's take a look at usage 2

Usage 2

The Create method is used to create the Demo object, and the Return method is to throw the Demo object back into the Object Pool (borrowed and returned).



The object pool is defined here to hold only one object.

Since there is a one-step return operation after taking it out of the object pool, item1 and item2 should be the same object.

After taking item2 out of the object pool, it is not returned, so the object pool will create a new object based on the policy we defined.

Here is the output result of usage 2:

985b3232-0a45-4115-8480-ad3d42c0ae10-itsvse.com-4/15/2020 3:31:15 AM
985b3232-0a45-4115-8480-ad3d42c0ae10-itsvse.com-4/15/2020 3:31:15 AM
True
8912424a-15c5-4891-b625-25b17eee5c8b-itsvse.com-4/15/2020 3:31:15 AM
False
You can see that the individual properties of item1, item2 and item3 are the same, and item1 and item2 are indeed the same object. Item3 and item1 are not the same thing.


Usage 3

You can see that item1 and item2 are the same object. When fetching an object from the Object Pool, the first one will be taken, so if you return it and retrieve it again, you will still get the original first.



item3 is taken directly from the Object Pool there and is not created again, because the Object Pool here maintains multiple objects, not just one in usage 2, so it is taken directly from the Pool.

Here is the output result

f3cd5467-536b-4ffe-9c71-de53027b4869-itsvse.com-4/15/2020 3:33:58 AM
f3cd5467-536b-4ffe-9c71-de53027b4869-itsvse.com-4/15/2020 3:33:58 AM
True
b933b593-af6d-4ebe-b21b-e8784d124764-itsvse.com-4/15/2020 3:33:58 AM
False
Same as usage 2, the essence is the same.

Usage 4

It seems that the above usage is not so similar to our normal use. We still need to rely on injection.

So let's finally look at how to combine dependency injection. Of course, the essence here is still inseparable from the two things of Policy and Provider.



We first need to register the Provider, and then directly take its instance to create an Object Pool.

If you want to use it elsewhere, you can inject it through the constructor.

The result here is also the same as before, there is not much to say.

summary

But no matter what kind of usage it is, we need to understand that Object Pool is inseparable from the three guys of Pool, Policy and Provider.

With these three, maybe we can do whatever we want.

Of course, it also offers several special things that you can check out if you are interested.

LeakTrackingObjectPool

StringBuilderPooledObjectPolicy





Previous:Introduction to Web Services Enhancements (WSE) 3.0
Next:How does Visual Studio 2013 find content that replaces multiple lines?
 Landlord| Posted on 2023-8-30 18:34:07 |
DefaultObjectPool is used on the underlying levelConcurrentQueueThread security queue implementation, the source code is as follows:


The hyperlink login is visible.


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com