.NET memory management

Everything DG Kernel: Technical discussions and issues
Post Reply
Mike Zubovic
Posts: 39
Joined: Mon Jul 15, 2024 5:20 am

.NET memory management

Post by Mike Zubovic »

Hello all

As you know, .NET uses garbage collection for freeing memory. GC uses some complex algorithm as to when and how it does the job. It actually changes between .NET versions. From our point of view, it should be considered random.

Say, you have code

Code: Select all

void f()
{
	int sz = EstimateMeshSize();
}

int EstimateMeshSize()
{
	IModel_DG iModelStandalone = m_generator.Create<IModel_DG>();
	iModelStandalone.Load("LargeMesh.stl");
	int simplexCount;
	// .... Access the mesh and assign simplexCount
	return simplexCount;
}
On return from EstimateMeshSize() DGK in ActiveX-based apps destroys iModelStandalone and all related objects because the application does not keep any references to it.

It is not what is happening by default with pure .NET DGKC controls. GC does not release the memory immediately. Often this can be observed in PC's performance monitors. Sometimes the memory is released only when the application closes.

It often makes sense and should not be a concern. Why reduce performance if there is plenty of memory? We need to trust that GC is smart enough.

If you do prefer the memory to be release immediately, add GC.Collect(); line in your code like:

Code: Select all

void f()
{
	int sz = EstimateMeshSize();
	GC.Collect();
}
See also Memory issues thread.

Happy coding
Post Reply