More Weird Behavior from the Draft WB: Memory Leak?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
yorik
Site Admin
Posts: 11547
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby yorik » Fri Feb 09, 2018 5:10 pm

How weird! :?
The culprit is this little block of code: https://github.com/FreeCAD/FreeCAD/blob ... #L350-L354

Code: Select all

if not self.lastObj[1]:
    self.lastObj[1] = obj.Name
elif self.lastObj[1] != obj.Name:
    self.lastObj[0] = self.lastObj[1]
    self.lastObj[1] = obj.Name
If I remove this block, no more memory leak. I tried to change it with this, it still produces a memory leak:

Code: Select all

if not self.lastObj[1]:
    self.lastObj = [None,obj.Name]
elif self.lastObj[1] != obj.Name:
    self.lastObj = [self.lastObj[1],obj.Name]
Crazy! I'll try other solutions...

*EDIT* No, the culprit is something else triggered by this code... Investigating further
User avatar
yorik
Site Admin
Posts: 11547
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby yorik » Tue Feb 13, 2018 9:10 pm

yorik wrote:
Fri Feb 09, 2018 5:10 pm
Investigating further
I think I got something here, can someone else test? This makes memory usage jump by ~30Mb here...

Code: Select all

import Part
edge = Part.LineSegment(FreeCAD.Vector(0,0,0),FreeCAD.Vector(2,2,2)).toShape()
for i in range(1000000):
    c = edge.Curve
wmayer
Site Admin
Posts: 14869
Joined: Thu Feb 19, 2009 10:32 am

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby wmayer » Tue Feb 13, 2018 9:56 pm

yorik wrote:
Tue Feb 13, 2018 9:10 pm
yorik wrote:
Fri Feb 09, 2018 5:10 pm
Investigating further
I think I got something here, can someone else test? This makes memory usage jump by ~30Mb here...

Code: Select all

import Part
edge = Part.LineSegment(FreeCAD.Vector(0,0,0),FreeCAD.Vector(2,2,2)).toShape()
for i in range(1000000):
    c = edge.Curve
I can confirm that the memory usage goes up by ~25MB. But when repeating this snippets several times the memory usage stays at the same level.
And the same behaviour can be observed when running this little snippet:

Code: Select all

for i in range(1000000):
    c = i
So, this means it's something completely outside the scope of FreeCAD and is purely Python related.
cflin
Posts: 49
Joined: Sun May 14, 2017 10:21 pm

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby cflin » Tue Feb 13, 2018 11:29 pm

I can confirm that the memory usage goes up by ~25MB. But when repeating this snippets several times the memory usage stays at the same level.
And the same behaviour can be observed when running this little snippet:

Code: Select all

for i in range(1000000):
    c = i
So, this means it's something completely outside the scope of FreeCAD and is purely Python related.
I started observing this memory leak only a couple of weeks ago, though. It did not seem to be there before. In my case, memory use expands by about 1.5GB each time I try to snap at some points. (Snapping works on some multi-parts now.) Should some variables be explicitly deleted?

This behavior occurs even with the latest version of FreeCAD:
OS: Ubuntu 16.04.3 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13289 (Git)
Build type: None
Branch: master
Hash: 23fff2b1d3a066d335dea509694e8f83f859d16a
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: English/UnitedStates (en_US)
User avatar
sgrogan
Posts: 5412
Joined: Wed Oct 22, 2014 5:02 pm

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby sgrogan » Tue Feb 13, 2018 11:38 pm

cflin wrote:
Tue Feb 13, 2018 11:29 pm
I started observing this memory leak only a couple of weeks ago, though.
cflin wrote:
Tue Feb 13, 2018 11:29 pm
Python version: 2.7.12
Where is Python from? https://packages.ubuntu.com/xenial/python
cflin
Posts: 49
Joined: Sun May 14, 2017 10:21 pm

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby cflin » Wed Feb 14, 2018 12:12 am

That's an excellent question! I don't know how to answer it. I have the Anaconda virtual environment installed for my Python needs, but there I have only Python3. Yes, Ubuntu states 2.7.11-1 as their official version, not the 2.7.11-2 that shows up in my system. 'apt cache policy python' shows:

python:
Installed: 2.7.11-1
Candidate: 2.7.11-1
Version table:
*** 2.7.11-1 500
500 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 Packages
100 /var/lib/dpkg/status


I am baffled. The only thing I can say is "congratulations for your astute observation." I have no answer, however. Does FreeCAD come with its own Python? My FreeCAD17 Python console shows:

Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type 'help', 'copyright', 'credits' or 'license' for more information.
>>>


Ditto for my FC16 Python console.
User avatar
sgrogan
Posts: 5412
Joined: Wed Oct 22, 2014 5:02 pm

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby sgrogan » Wed Feb 14, 2018 12:38 am

cflin wrote:
Wed Feb 14, 2018 12:12 am
Does FreeCAD come with its own Python?
If your using the freecad-daily PPA it should pull in the standard Ubuntu package. 2.7.11-1 or 2.7.11-2 should not be reported as 2.7.12 in "About FreeCAD".
Any chance at some point you added a Python2 PPA to your software sources?
cflin
Posts: 49
Joined: Sun May 14, 2017 10:21 pm

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby cflin » Wed Feb 14, 2018 2:02 am

If your using the freecad-daily PPA it should pull in the standard Ubuntu package. 2.7.11-1 or 2.7.11-2 should not be reported as 2.7.12 in "About FreeCAD".
Any chance at some point you added a Python2 PPA to your software sources?
I hear you. I looked through my '/etc/apt/' directory and I did not catch any Python ppa. Plus, python.org shows 2.7.14 as the latest version, so if I had a Python ppa, it probably would not be stuck at 2.7.12. Now, I don't remember adding or deleting a Python ppa. Even then, apt-get should be aware of the existence of 2.7.12. And I really doubt I installed Python 2.7.12 from source because I try to avoid interfering with apt-get. And my Anaconda installation is local, specifically not to interfere with the distribution's Python. And I am the only one—or so I think!—who has has ever had root privileges on this computer. So, this is a great mystery to me. It will take me some time to figure this out. Maybe my computer got hacked? LOL, I can't help but think of such a possibility because I really cannot explain how Python 2.7.12 ended up on my system, while 'apt cache policy' dishes out 2.7.11! Again, excellent catch!

As far as this thread is concerned, I think there is a memory leak caused by the Draft Workbench.
User avatar
sgrogan
Posts: 5412
Joined: Wed Oct 22, 2014 5:02 pm

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby sgrogan » Wed Feb 14, 2018 2:18 am

cflin wrote:
Wed Feb 14, 2018 2:02 am
As far as this thread is concerned, I think there is a memory leak caused by the Draft Workbench.
wmayer wrote:
Tue Feb 13, 2018 9:56 pm
So, this means it's something completely outside the scope of FreeCAD and is purely Python related.
I'm not qualified to differentiate. It seems there is a leak that is accelerated by your (Py)environment?
Maybe pip? Maybe I'm chasing a red-herring, but wmayer's post and the inconsistent Python version leads me down this rabbit hole.
wmayer
Site Admin
Posts: 14869
Joined: Thu Feb 19, 2009 10:32 am

Re: More Weird Behavior from the Draft WB: Memory Leak?

Postby wmayer » Wed Feb 14, 2018 9:28 am

Just to be clear I don't think that the huge leak we have seen is caused by Python. Only the minor memory consumption of 25-30 MB is caused by Python as shown with the code snippet but because it does this only once and not each time it's executed we can hardly call this a leak.