Enrollment 28.2.0
Loading...
Searching...
No Matches

User is able to use C# classes generated by SWIG tool.

Memory management

The C# Garbage Collector (GC) manages only managed memory, ensuring efficient allocation and deallocation of resources within that space.

In the case of the enrollment-sdk, objects in managed memory are typically small. However, these objects often create significantly larger amounts of unmanaged memory, contributing to the overall process memory usage.

Since managed objects are small, the GC is triggered less frequently, which can lead to high process memory consumption due to unmanaged memory growth.

The following sections outline solutions to address this challenge. You can choose one or more approaches that best suit your use case.

Use small heap

If your C# application uses only small objects, consider setting the maximum GC heap memory as low as possible using the HeapHardLimit configuration.

This will cause the GC to run more frequently, ensuring that unmanaged memory is released more effectively.

Use using statement

Use the using statement for each enrollment-sdk variable individually. Avoid chaining multiple enrollment-sdk variables in a single using statement.

CodeCode with using
// Please note that the sample source code
// provided here is for informational purposes
// only and should not be used for production
// purposes without proper testing and modification.
private static Face GetFace(byte[] img)
{
var capture = new FaceCapture(Image.Decode(new Bytes(img)));
var cfg = new FaceDetector.Config();
var detector = new FaceDetector(cfg);
var faces = capture.DetectWith(detector);
if (faces.Count == 0)
return default;
var face = faces[0];
return face;
}
// Please note that the sample source code
// provided here is for informational purposes
// only and should not be used for production
// purposes without proper testing and modification.
private static Face GetFace(byte[] img)
{
using var bytes = new Bytes(img);
using var image = Image.Decode(bytes);
using var capture = new FaceCapture(image);
using var cfg = new FaceDetector.Config();
using var detector = new FaceDetector(cfg);
using var faces = capture.DetectWith(detector);
if (faces.Count == 0)
return default;
// IMPORTANT
// Because the Face object is returned,
// we can not use `using` because
// `using` will delete native memory
// on function return
var face = faces[0];
return face;
}

Force GC

// Please note that the sample source code provided here is for informational purposes
// only and should not be used for production purposes without proper testing and
// modification.
static void TriggerGarbageCollector()
{
// Set process memory limit to 2GB
long maxMemoryLimit = 2L * 1024 * 1024 * 1024;
while (true)
{
try
{
long totalMemory = Process.GetCurrentProcess().WorkingSet64;
if (totalMemory > maxMemoryLimit)
{
// Trigger garbage collection
GC.Collect();
}
// Check every second
Thread.Sleep(1000);
}
catch (ThreadInterruptedException)
{
break;
}
}
}

Use it in main function of your application

var memoryLimiter = new Thread(new ThreadStart(TriggerGarbageCollector));
memoryLimiter.Start();

Exception Propagation

Exception from C# implementation of Interface Exception in Enrollment SDK native code Exception propagated back to C#
EnrollmentException and its subclasses EnrollmentException and its subclasses EnrollmentException and its subclasses
ApplicationException std::runtime_error ApplicationException
ArithmeticException std::runtime_error ApplicationException
DivideByZeroException std::domain_error ArgumentException
IndexOutOfRangeException std::out_of_range IndexOutOfRangeException
InvalidCastException std::runtime_error ApplicationException
InvalidOperationException std::logic_error ApplicationException
IOException std::ios_base::failure ApplicationException
NullReferenceException std::runtime_error ApplicationException
OutOfMemoryException std::runtime_error ApplicationException
OverflowException std::overflow_error OverflowException
SystemException std::runtime_error ApplicationException
ArgumentException std::invalid_argument ArgumentException
ArgumentNullException std::invalid_argument ArgumentException
ArgumentOutOfRangeException std::invalid_argument ArgumentException
Exception std::runtime_error ApplicationException