![]() |
VOOZH | about |
dotnet add package CodeProject.ObjectPool.MicrosoftExtensionsAdapter --version 6.5.0
NuGet\Install-Package CodeProject.ObjectPool.MicrosoftExtensionsAdapter -Version 6.5.0
<PackageReference Include="CodeProject.ObjectPool.MicrosoftExtensionsAdapter" Version="6.5.0" />
<PackageVersion Include="CodeProject.ObjectPool.MicrosoftExtensionsAdapter" Version="6.5.0" />Directory.Packages.props
<PackageReference Include="CodeProject.ObjectPool.MicrosoftExtensionsAdapter" />Project file
paket add CodeProject.ObjectPool.MicrosoftExtensionsAdapter --version 6.5.0
#r "nuget: CodeProject.ObjectPool.MicrosoftExtensionsAdapter, 6.5.0"
#:package CodeProject.ObjectPool.MicrosoftExtensionsAdapter@6.5.0
#addin nuget:?package=CodeProject.ObjectPool.MicrosoftExtensionsAdapter&version=6.5.0Install as a Cake Addin
#tool nuget:?package=CodeProject.ObjectPool.MicrosoftExtensionsAdapter&version=6.5.0Install as a Cake Tool
👁 License: MIT
👁 Donate
👁 Docs
👁 NuGet version
👁 NuGet downloads
👁 standard-readme compliant
👁 GitLab pipeline status
👁 Quality gate
👁 Code coverage
👁 Renovate enabled
A generic, concurrent, portable and flexible Object Pool for .NET, completely based on the Code Project article of Ofir Makmal.
Library is feature complete and no further development is planned on this project, except for routine maintenance and bug fixes.
Original source code has been modified, in order to introduce:
Microsoft.IO.RecyclableMemoryStream library
from Microsoft. It eliminates Large Object Heap allocations, which this library does not do.Moreover, a few unit tests have been added, in order to improve code reliability, and a lot of other small changes have also been applied.
Of course, all modified source code is freely available in this repository.
Many thanks to Ofir Makmal for his great work.
NuGet package CodeProject.ObjectPool is available for download:
dotnet add package CodeProject.ObjectPool
An adapter for Microsoft.Extensions.ObjectPool is also available on NuGet:
dotnet add package CodeProject.ObjectPool.MicrosoftExtensionsAdapter
Quick and dirty example:
/// <summary>
/// Example usages of ObjectPool.
/// </summary>
internal static class Program
{
/// <summary>
/// Example usages of ObjectPool.
/// </summary>
private static void Main()
{
// Creating a pool with a maximum size of 25, using custom Factory method to create and
// instance of ExpensiveResource.
var pool = new ObjectPool<ExpensiveResource>(25, () => new ExpensiveResource(/* resource specific initialization */));
using (var resource = pool.GetObject())
{
// Using the resource...
resource.DoStuff();
} // Exiting the using scope will return the object back to the pool.
// Creating a pool with wrapper object for managing external resources, that is, classes
// which cannot inherit from PooledObject.
var newPool = new ObjectPool<PooledObjectWrapper<ExternalExpensiveResource>>(() =>
new PooledObjectWrapper<ExternalExpensiveResource>(CreateNewResource())
{
OnReleaseResources = ExternalResourceReleaseResource,
OnResetState = ExternalResourceResetState
});
using (var wrapper = newPool.GetObject())
{
// wrapper.InternalResource contains the object that you pooled.
wrapper.InternalResource.DoOtherStuff();
} // Exiting the using scope will return the object back to the pool.
// Creates a pool where objects which have not been used for over 2 seconds will be
// cleaned up by a dedicated thread.
var timedPool = new TimedObjectPool<ExpensiveResource>(TimeSpan.FromSeconds(2));
using (var resource = timedPool.GetObject())
{
// Using the resource...
resource.DoStuff();
} // Exiting the using scope will return the object back to the pool and record last usage.
Console.WriteLine($"Timed pool size after 0 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 1
Thread.Sleep(TimeSpan.FromSeconds(4));
Console.WriteLine($"Timed pool size after 4 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 0
// Adapts a timed pool to Microsoft Extensions abstraction.
var mPool = ObjectPoolAdapter.CreateForPooledObject(timedPool);
// Example usage of Microsoft pool.
var mResource = mPool.Get();
Debug.Assert(mResource != null);
mPool.Return(mResource);
// Adapts a new pool to Microsoft Extensions abstraction. This example shows how to adapt
// when object type does not extend PooledObject.
var mPool2 = ObjectPoolAdapter.Create(new ObjectPool<PooledObjectWrapper<MemoryStream>>(
() => PooledObjectWrapper.Create(new MemoryStream())));
// Example usage of second Microsoft pool.
var mResource2 = mPool2.Get();
Debug.Assert(mResource2 != null);
mPool2.Return(mResource2);
Console.Read();
}
private static ExternalExpensiveResource CreateNewResource()
{
return new ExternalExpensiveResource();
}
public static void ExternalResourceResetState(ExternalExpensiveResource resource)
{
// External Resource reset state code.
}
public static void ExternalResourceReleaseResource(ExternalExpensiveResource resource)
{
// External Resource release code.
}
}
internal sealed class ExpensiveResource : PooledObject
{
public ExpensiveResource()
{
OnReleaseResources = () =>
{
// Called if the resource needs to be manually cleaned before the memory is reclaimed.
};
OnResetState = () =>
{
// Called if the resource needs resetting before it is getting back into the pool.
};
}
public void DoStuff()
{
// Do some work here, for example.
}
}
internal sealed class ExternalExpensiveResource
{
public void DoOtherStuff()
{
// Do some work here, for example.
}
}
Starting from v4, Object Pool supports async pooled object initialization. Therefore, objects can be retrieved in two ways:
obj = pool.GetObject();
obj = await pool.GetObjectAsync();
Those methods depend on the factory method specified during pool initialization. Because making async factories "sync" is usually a problem, which can lead to deadlocks, we have the following situation:
| Factory type | GetObject |
GeObjectAsync |
|---|---|---|
| Not specified | OK | OK, uses a result task |
| Sync | OK | OK, uses a result task |
| Async | KO, throws an exception | OK |
So, to sum it up:
MRs accepted.
Small note: If editing the README, please conform to the standard-readme specification.
Visual Studio Code, with Remote Containers extension, is the recommended way to work on this project.
A development container has been configured with all required tools.
Visual Studio Community is also supported
and an updated solution file, object-pool.sln, has been provided.
When opening the development container, dependencies should be automatically restored.
Anyway, dependencies can be restored with following command:
dotnet restore
Tests can be run with following command:
dotnet test
Tests can also be run with following command, which collects coverage information:
./build.sh --target run-tests
MIT © 2013-2023 PommaLabs Team and Contributors
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 6.5.0 | 1,335 | 2/15/2024 |
| 6.4.0 | 266 | 11/16/2023 |
| 6.3.0 | 292 | 4/13/2023 |
| 6.2.0 | 8,258 | 12/18/2022 |
| 6.1.1 | 413 | 10/25/2022 |
| 6.1.0 | 2,989 | 11/20/2021 |
| 6.0.0 | 223 | 11/3/2021 |
| 5.0.5 | 237 | 11/3/2021 |
| 5.0.4 | 258 | 9/26/2021 |
| 5.0.3 | 811 | 6/13/2021 |
| 5.0.2 | 855 | 12/26/2020 |
| 5.0.1 | 706 | 11/14/2020 |
| 5.0.0 | 642 | 11/1/2020 |
| 4.0.2 | 1,787 | 2/23/2020 |
| 4.0.1 | 929 | 6/22/2019 |
| 3.2.4 | 890 | 6/13/2019 |
| 3.2.3 | 841 | 5/12/2019 |
| 3.2.2 | 5,371 | 10/28/2017 |
| 3.2.1 | 1,608 | 9/30/2017 |