Memory consumption (restored)

Everything DG Kernel: Technical discussions and issues
Post Reply
ron1234
Posts: 1
Joined: Thu Jul 04, 2024 12:45 am

Memory consumption (restored)

Post by ron1234 »

Hi Prashant

In the sample I have sent you yesterday It looks like there is a problem with memory. I do not see memory going down after I delete all objects from the model.

Is it an internal KernelCAD leak?

Thanks
Ron
Prashant Kande
Posts: 65
Joined: Tue Jun 18, 2024 6:12 am

Re: Memory consumption (restored)

Post by Prashant Kande »

I have debugged your sample. In this particular case it is caused by the fact that a reference to the removed section is not released (not set to Nothing) by the application.

Briefly the situation is this: You have a large mesh section object in the model and ISection m_iSectMesh member of the form class which controls it. You delete all objects using iarr. .RemoveAll() where IArray iarr is array of object in the model. The problem is that the memory consumption is not going down after that.

This is because after everything has been removed from the model the application still has a reference to the removed section stored in the m_iSectMesh member of the class. This reference is not released until the form is destructed on closing the application. KernelCAD knows that you keep the reference so it does not delete the section from memory, otherwise any use of m_iSectMesh would cause a crash.

Sections can exist without being added to any model. So KernelCAD assumes you need the section for something else when you call RemoveAll()
To fix the leak you just need to set m_iSectMesh to Nothign (null in C#, etc) when you do not need it anymore. I have checked this by adding m_iSectMesh = Nothing to Button2_Click() (delete button handler). With this after the delete button memory goes down almost to where it was before.

So, briefly set to Nothing all references to any interfaces. All other interfaces are released automatically because they are acquired inside of scope of each method. So they are released by .NET when the method is exited. m_iSectMesh is different because is member of the class which stays alive during whole application life.

KC does have leaks in some situations when internally some memory not reference-counted properly, but we believe they are small and happen relatively rarely.

Regards
Prashant Kande
Posts: 65
Joined: Tue Jun 18, 2024 6:12 am

Re: Memory consumption (restored)

Post by Prashant Kande »

Generally about memory leaks:

They are not obvious in .NET as it hides memory handling. Small leaks are not a problem. It becomes a problem when they are accumulating when the app runs for a while. You can notice that the app becomes sluggish. This is because Windows never run out of memory but start swapping intensively to the disk.

When you get the suspicion open Performance tab in the task manager and watch the memory for few minutes. If it climbs steadily without going down you have a problem

To fix it try to think first what objects are being constructed but not released. Any interfaces you acquire inside a method are released by .NET when it goes out of scope:
Void MyMethod() {IModel imod – m_kc.GetModel(); imod.Update() }
imod here would be released when MyMethod() is exited.

So the main suspects are the interfaces stored in members of a class which is not being destroyed on in global scope. Just set them to null (Nothing)
Prashant Kande
Posts: 65
Joined: Tue Jun 18, 2024 6:12 am

Update July 2024

Post by Prashant Kande »

FYI KerenelCAD mentioned here is the old DG Kernel. Section is the old entity. Otherwise, the information is still relevant.
Post Reply