CLI Original objects stuck in gen2, not garbage collection

I am working on this high frequency production system. There is a C # / CLI layer that calls the C ++ library. We observe that managed objects go into Generation 2 of the garage collector and become stuck. In the end, C # applications stop when RAM ends. These managed entities are local entities and must have a very short lifespan. They are also mentioned only once. C # applications must call .Dispose () for all of their objects that contain their own resources so that everything is forcibly deleted. We have quite a few objects, so this is not ideal, but from the point of view of the API - randomly. The CLI is as follows:

Field::~Field() { if(m_pField != NULL) { delete m_pField; m_pField = NULL; } System::GC::SuppressFinalize(this); } Field::!Field() { if(m_pField != NULL) { delete m_pField; } } 

Can anyone think why these short-lived objects never gather and free up memory?

0
source share
1 answer

The problem is unmanaged objects that do not take into account the "memory pressure" value that the GC uses to decide when to do garbage collection.

One thing you can do is use GC.AddMemoryPressure( to tell GC that there is a large unmanaged object associated with your managed shell.

 Field::Field() { //... Other stuff if(m_pField != NULL) { m_lSizeOfField = GetSizeOfField(m_pField); System::GC::AddMemoryPressure(m_lSizeOfField); } } Field::~Field() { //If you had managed objects you would call "delete" here on them. //delete m_managedData; //In C++/CLI if you have unmanged resources just have the dispose method // call the finalizer. It is cleaner and easier to maintain. // You can't do this in C# this->!Field(); //No need to add this next line, C++/CLI does it for you. //System::GC::SuppressFinalize(this); } Field::!Field() { if(m_pField != NULL) { delete m_pField; m_pField = NULL; System::GC::RemoveMemoryPressure(m_lSizeOfField); } } 
+3
source

Source: https://habr.com/ru/post/928202/


All Articles