An Effective tool for unmanaged array in C#.
Array in C# is allocated in managed memory.
UnmanagedArray<T>
in this library is allocated in unmanaged memory.
In other words, items in UnmanagedArray<T>
is not collected by Garbage Collection.
The only type of item UnmanagedArray<T>
supports is unmanaged
type.
unmanaged
type is int
, float
, recursive-unmanaged struct, and so on.
string
, and other types which are class
are NOT SUPPORTED.
(because reference types are allocated in managed memory on C#.)
$ git clone https://github.com/ikorin24/UnmanagedArray.git
$ cd UnmanagedArray
$ dotnet build src/UnmanagedArray/UnmanagedArray.csproj -c Release
Install from Nuget by package manager console (in Visual Studio).
https://www.nuget.org/packages/UnmanagedArray
PM> Install-Package UnmanagedArray
The way of use is similar to normal array.
Unmanaged resources are release when an instance goes through the scope.
using UnmanageUtility;
// UnmanagedArray releases its memories when it goes through the scope.
using(var array = new UnmanagedArray<int>(10))
{
for(int i = 0;i < array.Length;i++)
{
array[i] = i;
}
}
If not use the using
scope, you can release the memories by Dispose()
method.
var array = new UnmanagedArray<int>(10);
array[3] = 100;
array.Dispose(); // The memories allocated in unmanaged is released here.
Of cource, LINQ is supported.
using(var array = Enumerable.Range(0, 10).ToUnmanagedArray())
using(var array2 = array.Where(x => x >= 5).ToUnmanagedArray()) {
for(int i = 0; i < array2.Length; i++) {
Console.WriteLine(array2[i]); // 5, 6, 7, 8, 9
}
}
NOTICE
UnmanagedArray<T>
has Finalizer and releases its unmanaged resources automatically when you forget releasing that.
However, you have to release them explicitly ( by using
scope or Dispose()
).
UnmanagedList<T>
is available, which the way of use is similar to List<T>
.
using(var list = new UnmanagedList<int>())
{
list.Add(4);
list.Add(9);
foreach(var num in list)
{
Console.WriteLine(num);
}
}
This is under MIT license.
This software includes the work that is distributed in the Apache License 2.0.
Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
- First release
- Performance improvement of iteration by
foreach
, that is as faster as T[] (normal array).
- Great performance improvement of accessing to the item by index. (
array[i]
)
- Performance improvement.
- Add
GC.AddMemoryPressure
in constructor andGC.RemoveMemoryPressure
in destructor.
- Change namespace into
UnmanageUtility
.
- Add
UnmanagedList<T>
.
- Performance improvement.
- Add some methods.
- Add
UnmanagedArray<T>.Empty
static property. - Add property setter of
UnmanagedList<T>.Capacity
.
- Add
UnmanagedArray<T>.AsSpan
overload methods. - Package for multi target frameworks. (net48, netcoreapp3.1, net5.0, netstandard2.0, netstandard2.1)
- Fix small bugs.
- Add
UnmanagedList<T>.Extend
method.