Face.Surface : memory leak ?

Need help, or want to share a macro? Post here!
User avatar
cristian.a73
Posts: 33
Joined: Wed Jul 18, 2018 4:15 pm

Face.Surface : memory leak ?

Postby cristian.a73 » Thu Jan 10, 2019 8:52 pm

Hi FreeCAD Forumer

In my model, about 6000 faces from step my code is something like this :

Code: Select all

for f in Shape.faces:
 sf = f.Surface
 del sf
The FreeCADCmd run until all 4gb of free Ram was used (aprox 20kb per second), than crash with message about no memory available
How could access to Face.Surface class without cause a memory leak ?
Thank you
Cristian

OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 32-bit
Version: 0.18.15527 (Git)
Build type: Release
Branch: master
Hash: 70aaf3e95fe2a3f17d43e95a1afe7d903d91b8ae
Python version: 2.7.14
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: Italian/Italy (it_IT)
wandererfan
Posts: 2569
Joined: Tue Nov 06, 2012 5:42 pm

Re: Face.Surface : memory leak ?

Postby wandererfan » Fri Jan 11, 2019 3:44 am

cristian.a73 wrote:
Thu Jan 10, 2019 8:52 pm

Code: Select all

for f in Shape.faces:
 sf = f.Surface
 del sf
Maybe you need to force garbage collection? del isn't guaranteed to free up memory immediately.

Code: Select all

import gc
gc.collect()
User avatar
cristian.a73
Posts: 33
Joined: Wed Jul 18, 2018 4:15 pm

Re: Face.Surface : memory leak ?

Postby cristian.a73 » Fri Jan 11, 2019 10:28 am

wandererfan wrote:
Fri Jan 11, 2019 3:44 am
Maybe you need to force garbage collection? del isn't guaranteed to free up memory immediately.

Code: Select all

import gc
gc.collect()
Unfortunally gc has no effect :cry:
User avatar
DeepSOIC
Posts: 6522
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Face.Surface : memory leak ?

Postby DeepSOIC » Fri Jan 11, 2019 11:01 am

I think I can confirm.

0. install Lattice2
1. create polar array
2. set Count property to 10000
-> memory usage is 190 MB
3. paste this to console, multiple times:

Code: Select all

sh = App.ActiveDocument.ActiveObject.Shape
for f in sh.Faces:
 sf = f.Surface
 del sf

after 10 runs, memory usage is 500 MB

same will probably work with a Draft Array of boxes, but it consumes 1G of memory just by itself, so it's harder to notice.

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15518 (Git)
Build type: Release
Branch: master
Hash: e83c44200ab428b753a1e08a2e4d95e03236e481
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Russian/Russia (ru_RU)
User avatar
DeepSOIC
Posts: 6522
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Face.Surface : memory leak ?

Postby DeepSOIC » Sun Jan 13, 2019 10:11 am

User avatar
DeepSOIC
Posts: 6522
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Face.Surface : memory leak ?

Postby DeepSOIC » Sun Jan 13, 2019 10:37 am

This might be an OCC leak... If I just use Surface attribute many many times, memory use doesn't grow.

Code: Select all

import Part
cub = Part.makeBox(10,10,10)
f = cub.Faces[0]
for i in range(10000000):
    sf = f.Surface

User avatar
DeepSOIC
Posts: 6522
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Face.Surface : memory leak ?

Postby DeepSOIC » Sun Jan 13, 2019 10:43 am

And this leaks:

Code: Select all

import Part
cub = Part.makeBox(10,10,10)
f = cub.Faces[0]
for i in range(10000000):
    sf = cub.Faces[0].Surface

wmayer
Site Admin
Posts: 14045
Joined: Thu Feb 19, 2009 10:32 am

Re: Face.Surface : memory leak ?

Postby wmayer » Sun Jan 13, 2019 3:32 pm

It's a problem of the Python wrappers that create some cyclic dependencies. This causes the ref counter never to become zero and thus the memory is not freed.
User avatar
cristian.a73
Posts: 33
Joined: Wed Jul 18, 2018 4:15 pm

Re: Face.Surface : memory leak ?

Postby cristian.a73 » Tue Jan 15, 2019 9:53 am

:idea: ... a possible workaround :

Code: Select all

sh = App.ActiveDocument.ActiveObject.Shape
sfArr = []
for f in sh.Faces:
 sfArr.append(f.Surface)

for i in range(0,1000000):
 for sf in sfArr:
  print "sf:"+str(sf)
  ...