Clearing mesh from memory

Hi,

I have a whole lot of variables with large meshes in them, in this is causing my computer to run out of memory. But I’m using the meshes one at a time, so I could clear the variable of the mesh data, freeing up memory, I think… I’ve been trying to do this by using mesh.dispose, but that doesn’t seem to do the trick. Any suggestions?

Thanks,
Sam

Hi Sam ,

What language, I think that’s relevant.
My guess is you might need to dispose of the variable as well.

-Willem

Hi Willem,

VB.net using Rhinocommon.

The variable belongs to a class, and that class is an instance in a list, and that list belongs to the plugin class, if all that makes any difference…

Thanks,
Sam

Dispose should do the trick, but maybe you also need to do GC.Collect().

But, if you use the meshes one at a time, why not reuse the same mesh object over and over? That should keep the memory consumption low.

Not sure, but you may have to implement IDisposable as well.

I see your point Menno, though there are occasions when I don’t want to dispose the meshes, so that complicates things.

I would rather just find a way to clear the variable and free up the memory when I don’t need to save the mesh data.

I tried GC.collect, but that didn’t help. Any other suggestions? Would Idisposable help (I’m not sure how to go about using that…)?

Thanks,
Sam

Hi Guys,

Still trying to figure this one out. I’m taking Menno’s suggestion and reusing the same variable to store the meshes. I’ve made the mesh variable a shared variable in a class. If I understand correctly, the shared variable is just one variable regardless of how many instances of the class are created, and so when I populate the shared variable with a new mesh, the old mesh gets cleared from memory. Does this make sense?

But even after trying this, I’m still finding I’m running out of memory. I could make a lot of other variables similarly shared, but before I undertake that work, I’m hoping someone can tell me that my reasoning is sound.

Or perhaps there is something else I should be doing to avoid the gradual creap in memory usage as my plugin continues to run a rather long command. Below is a screenshot of the heap, if that’s any help, or perhaps there is a better way to diagnose memory issues?

Any help would be greatly appreciated,
Thanks,
Sam

Only if you call Dispose on the old mesh right before you reassign it.

OK, I understand.

I’ve done some more digging, and I think what’s causing the problem is that I am deleting objects and then adding transformed versions of them back to the document. I’m doing this in loops within loops within loops, so a lot of itterations all in a single command. And every time I delete and add an object back, memory usage creeps upwards. Please see the code below to see what I mean. Any suggestions on how I can work around this issue will still executing it in a single command?

Thanks,
Sam

Dim Sphere = New Sphere(Point3d.Origin, 10).ToBrep
Dim SphereID = doc.Objects.AddBrep(Sphere)
For i = 0 To 9999
Dim xform = Transform.Translation(New Vector3d(0, 0, 0.1))
Sphere.Transform(xform)
doc.Objects.Delete(SphereID, True)
SphereID = doc.Objects.AddBrep(Sphere)
doc.Views.Redraw()
Next

This looks like you are trying to animate a sphere across the screen. A display conduit is a much better solution for this type of problem.

Hi Steve,

My example was over simplified… I’m actually modifying the object then adding it back, sometimes using a spacemorph. Can that be done in a display conduit? If not, thoughts on how to deal with the memory issue?

Thanks,
Sam

Why are the iterations there? Is the purpose to animate something on the screen? If that is the purpose, then a conduit is the way to go.

I’m batching simulations, but you’re questions helped me realize that I don’t need to provide visualization for long batches, so I’ll just not add and delete geometry incessantly. It’s only single simulations where visualization is important. I’ll try to fix it up. Depending on how it goes, I might try conduits at some point for single simulations where visualization is important.

Thanks for the awesome support Steve!
Sam

why not reuse the same mesh object over and over?

I’m taking Menno’s suggestion and reusing the same variable

Please note the difference between object and variable. If you assign a new mesh to the old variable, you are not reusing the object. What I meant is that you have one mesh object, whose vertices and faces you keep changing. Seeing your other posts however, I wonder if this is really the issue.

Hi Menno,

The issue turned out to be adding and removing objects from the document many times. I’ve eliminated this for big batches and runs without the memory issues.

Thanks guys!
Sam