MEMORY LEAK - Interactive window Python 2.7 (PTVS 2.0, VS212 Ultimate)

Jul 17 at 2:00 PM
Hi,

I installed PTVS 2.0 VS2012, but when creating any python project or simply opening Python 2.7 interactive, memory of the process devenv.exe goes >3GB, until VS crashes.

Can this be a problem related to the setting of some enviromental variables on my machine?

What can be the cause of this HUGE memory leak?

I would appreciate some help because I would like to use PTVS!

thanks
Coordinator
Jul 17 at 2:13 PM
What environment variables have you ser? We mostly force Python to ignore the problematic ones, but there may be something we missed. PtVS doesn't read any itself.

Also, where did you get Python from and what packages do you have installed?

You could also try 2.1 Beta 2, which has been pretty stable so far.
Jul 17 at 2:35 PM
Hi

thanks for the quick reply.

I am using Python 2.7.5 for Windows, downloaded from https://www.python.org/download/releases/2.7.5/


PYTHONPATH is not empty because I have other projects that require this variable to be set. Can this be an issue?

Furthermore the path C:\Python27\ is set as ENVIROMENTAL VARIABLE in Path and PYTHONPATH.
Jul 17 at 3:04 PM
Hi Use also these other packages

1) comtypes
2) eGenix
3) matplotlib
4) numpy
5) psutil
6) psycopg2
7) py2exe
8) pyparsing
9) python-dateutil-1.5
10) pywin32
11) scipy
Jul 17 at 4:12 PM
I just installed 2.1 Beta 2.



Without creating any new Python Project I simply checked Python Environments.

I hit the button refresh "Completion DB", and the process "Python Tools for Visual Studio background analyzer" kicked in taking almost 700MB of memory.

Is this memory issue related to some DB caching, or intellisense?
Coordinator
Jul 17 at 4:39 PM
The background analyzer has to use that much memory because it's doing a lot of work (see our Intellisense documentation), but it will only run once and the results are cached.

The directory that contains python.exe never needs to be in PYTHONPATH, and PTVS will ignore it anyway (though there's now an option in 2.1b2 that lets you disable that). But it shouldn't be causing any particular issues here.

Do you still see the memory leak in devenv.exe with 2.1 Beta 2? That sounds like a real problem, but it may have been fixed since 2.0.
Jul 17 at 5:45 PM
Hi


The problem appears just when I create a simple project with a Python application.
The empty project is not immediately visible is the solution explorer and the memory keeps growing up to 3GB.

What type of info does an empty Python project needs to gather causing the memory to shoot up to 3GB?

Once the Python application is created the memory keeps growing until VS crashes around 3.5 GB. it happens that sometime memory suddenly dumps to a level of 1.8GB but I am not sure why.


Any idea or suggestions about what can cause this issue?

thanks
Coordinator
Jul 17 at 5:49 PM
We've certainly never seen anything like that. Do you have any other extensions installed? Some tools like Resharper don't necessarily know about Python projects and may do strange things when they encounter one.
Jul 21 at 12:01 PM
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\PythonProfiling\2.0\vspyprof.py", line 49, in profile
handle = start_profiling()
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\PythonProfiling\2.0\vspyprof.py", line 30, in start_profiling
return pyprofdll.InitProfiler(profiler)
OSError: exception: access violation writing 0x000000B4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\PythonProfiling\2.0\proflaun.py", line 35, in <module>
vspyprof.profile(__file__, globals(), locals(), profdll)
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\PythonProfiling\2.0\vspyprof.py", line 55, in profile
pyprofdll.CloseThread(handle)
UnboundLocalError: local variable 'handle' referenced before assignment
Press any key to continue . . .
Jul 21 at 12:17 PM
I am trying to figure out what is happening with your code.
There are indeed few memory leaks due to thrown exceptions of the type above.
Can you please tell me why other users of this tool are not experiencing the same issues? Thanks
Coordinator
Jul 21 at 5:15 PM
Memory leaks due to profiling are very unlikely because profiling runs in a separate process. It looks like you've found a bug in our profiling code though, so I'll make an issue for us to look into that. It may be caused by one of the packages you are using, which might explain why other people haven't seen the issue (or they may just have moved on and never reported it... :( ).

One possibility for the memory leak may be that PTVS is discovering a lot of loose Python files nearby and trying to load and analyze them all. This can easily lead to excessive memory usage since it doesn't have the same protections that we use for the completion DB analysis. It may be due to your existing PYTHONPATH value, though I don't think we were using that automatically in 2.0.

Could you try clearing PYTHONPATH and seeing if that makes a difference to the memory use?
Coordinator
Jul 21 at 5:16 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 21 at 6:03 PM
Hi,

thanks for the reply. I deleted PYTHONPATH. However the leak is still there. no difference.

However I see a difference when using different python environments.

I have installed 2 different Python environments: 2.7.5 and 3.4.1

The LEAK is huge when using 2.7.5 (memory grows quickly to 3GB ) and very mild when using 3.4.1.
Coordinator
Jul 21 at 6:06 PM
Does the leak happen while you are editing code (you mentioned earlier that it didn't require you to do anything, but now it sounds like you're using it)? We've had that one reported and largely fixed in PTVS 2.1, though there are still some issues that I'm working on.

If you have fewer libraries installed into 3.4.1 than 2.7.5, this is likely to be the same issue. But if you don't actually have to be editing code for it to leak, this is something else.
Jul 21 at 7:13 PM
Hi,

no, I don't need to edit the file. I just need to create an empty project or just use the interactive window to trigger the leak.


The main difference between the two environments I am using is that I have few additional packages installed and compatible with just Python 2.7.5 and not with 3.4.1.


Packages are all installed within thee folder C:\Python27.
Jul 21 at 7:15 PM
All this packages are within Python 2.7.5

1) comtypes
2) eGenix
3) matplotlib
4) numpy
5) psutil
6) psycopg2
7) py2exe
8) pyparsing
9) python-dateutil-1.5
10) pywin32
11) scipy
Coordinator
Jul 21 at 10:04 PM
I've tried setting up an environment with all of those packages but I can't reproduce the same issue.

If you look in C:\Users\<your username>\AppData\Local\Python Tools you'll find the place where we keep the database. It's possible that it was corrupted in some way that makes us allocate too much memory when we try and load it. I'm guessing a lot here, but I can't think of anything else that happens both when you create a project and open the interactive window.

You can try deleting the entire folder and regenerating the DB (just clicking the button in VS won't work because we try and avoid regenerating everything if possible). However, if you could keep a copy of all the files I'd be interested to check them out if they are actually the problem (you can email them to ptvshelp@microsoft.com or upload them somewhere public and email us a link - should be about 5-15MB in a ZIP/RAR/7Z file).
Jul 22 at 10:49 AM
I deleted the CompletionDB. The memory was ok until the python.tools.analyzer.exe started. it's memory went up to 1.6GB, then reduced. after that the memory of devenv.exe started to grow up to 3GB.


this is the Analysis log after I deleted the DB

FAIL_STDLIB: (-3) "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\Microsoft.PythonTools.Analyzer.exe" /id {2af0f10d-7135-4994-9156-5d01c9c11b7e} /version 2.7 /python C:\Python27\python.exe /library C:\Python27\lib /outdir "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\2af0f10d-7135-4994-9156-5d01c9c11b7e\2.7" /basedb "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\CompletionDB" /log "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\2af0f10d-7135-4994-9156-5d01c9c11b7e\2.7\AnalysisLog.txt" /glog "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\AnalysisLog.txt" /wait ""
This interpreter is already being analyzed.
2014-07-22T10:33:27 START_STDLIB "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\Microsoft.PythonTools.Analyzer.exe" /id {2af0f10d-7135-4994-9156-5d01c9c11b7e} /version 2.7 /python C:\Python27\python.exe /library C:\Python27\lib /outdir "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\2af0f10d-7135-4994-9156-5d01c9c11b7e\2.7" /basedb "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\CompletionDB" /log "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\2af0f10d-7135-4994-9156-5d01c9c11b7e\2.7\AnalysisLog.txt" /glog "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\AnalysisLog.txt" /wait ""
2014-07-22T10:38:24 DONE_STDLIB "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\Microsoft.PythonTools.Analyzer.exe" /id {2af0f10d-7135-4994-9156-5d01c9c11b7e} /version 2.7 /python C:\Python27\python.exe /library C:\Python27\lib /outdir "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\2af0f10d-7135-4994-9156-5d01c9c11b7e\2.7" /basedb "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\CompletionDB" /log "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\2af0f10d-7135-4994-9156-5d01c9c11b7e\2.7\AnalysisLog.txt" /glog "C:\Users\<user name>\AppData\Local\Python Tools\CompletionDB\11.0\AnalysisLog.txt" /wait ""
Coordinator
Jul 22 at 2:04 PM
Thanks, it sounds like that's the issue. Are you able to get a copy of the entire CompletionDB folder to me? This is a very unusual bug...
Jul 23 at 10:11 AM
There are some VS settings that clash badly with this PythonTools package.

I had to uninstall everything and run in the Command Prompt.

1) devenv /Resetsettings to eliminate the cause of corrupted settings.
2) devenv /ResetSkipPkgs
3) devenv /Safemode eliminate the possibility that third party Add-ins or packages are causing problems.
Aug 4 at 5:11 PM
Unfortunately I unable to send the CompletionDB over.... it is a ~30MB zipped file.

I can confirm that it is indeed the issue.

ISSUES that I experience:

1) When I create a new Python application project and there is an existing completionDB (based on Python 2.7 environment with several other installed packages)
VS2012 takes minutes to create the solution and the memory grows up to 2GB+> furthermore memory doesn't stop once the solution is ready and visible in the explorer, but keeps growing slowly until VS crashes.

2) When I open an existing python application I experience something similar as above.


HOWEVER if I delete my CompletionDB before creating a new application or opening an existing project I have milder issues. The projects are loaded almost instantaneously and I start to have memory leak only when I refresh the DB.

Any Idea on how to go around this issue?IS this beacuse i am using many python packages in my applications?

thanks for your help.
Coordinator
Aug 4 at 5:16 PM
if size is an issue, you can send it via onedrive.com or google drive as well...
Coordinator
Aug 4 at 6:07 PM
Yes, anywhere public you can upload the DB and email a link to ptvshelp@microsoft.com will be essential for this.

It's probably because of a particular Python package, and probably because we are misunderstanding something that it is doing and writing out part of the DB incorrectly. Without being able to see the files, we have no way of tracking this down other than uninstalling each package one by one and seeing which one fixes it (and that doesn't actually fix our bug...)
Coordinator
Aug 4 at 6:13 PM
perhaps we need to ship a utility for binary searching pkgs to find the one that gives analysis heartburn.
Aug 4 at 6:19 PM
HI

Thanks for quick reply. I am sending you an email @

I am sending you an Analysis log that reports quite a few errors. I am sending at ptvshelp@microsoft.com with subject "MEMORY LEAK - completionDB - Python 2.7"

most errors are of type:

2014-08-04T17:22:09: [WARNING] Errors
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\ExtensionScraper.py", line 43, in <module>
__import__(mod_name)
ImportError: This must be an MFC application - try 'import win32ui' first
2014-08-04T17:22:09: [ERROR] Failed to scrape dde (Exit code: 1)
Coordinator
Aug 4 at 6:32 PM
Thanks for the log. There are no errors showing up in there that would be the cause of a corrupted database so I can't be any more helpful.

If you can't send your database files somehow, as an alternative you could send the exact version number of every package you have installed. If you have PTVS 2.1 Beta 2, you can get these by right-clicking the environment in Solution Explorer and selecting "Generate requirements.txt" and then sending that file (see our documentation if you need more info on doing this) or else you can run pip freeze from the command line.
Aug 4 at 7:18 PM
I dropped you an email. The Generate requirements.txt doesnt seem to work.
Coordinator
Aug 4 at 8:21 PM
You probably don't have pip installed, which is required for that feature.

I set up an environment with those packages installed but cannot reproduce the issue. Without getting your actual DB, there's nothing I can do to diagnose the issue.
Aug 7 at 2:47 PM
Hi,

I sent you over the DB. I hope you can now diagnose the issue. Thanks!
Aug 7 at 5:07 PM
please let me know if you received the file. it is approx 5MB large.
Coordinator
Aug 7 at 5:10 PM
I've seen the file come in and it's the right size. Haven't had a chance to look yet.

You mention that you see memory usage increase every time you hit "Refresh DB" - does it actually do any refreshing or does it run really quickly? Also, does it keep increasing forever or does it eventually drop down? (Garbage collection should eventually clear things up, but probably not until you're using >1.5GB of RAM. If you can get past this point, then the issue is still there, otherwise this may just be normal behaviour and we'll need the full database to find the issue.)
Aug 7 at 5:28 PM
The refresh happens very quickly.
There is no increase in size of the database. The only files that change are database.ver and Analysis.log. but just their date. database content looks the same.



However the memory of devenv.exe steps up ~100MB per time.

And there are smaller increases in memory when I run the code in the interactive ~5-10MB per time.

I just kept hitting the button and I get up to 3GB and no memory drop. VS then crashes.

I sent you the entire database.
Aug 7 at 7:53 PM
I can see indeed an issue with garbage collection.

The issue is magnified when I load up big projects. all the time I hit Refresh DB, memory jumps up 300/400MB.

when I create python solutions from existing code, memory jumps up a lot. And furthermore I am the unable to open these large python solution
launching them directly from the .sln file. VS just crashes.


This happens just with Python. All other languages are OK..
Coordinator
Aug 7 at 7:55 PM
Thanks, I think I can see what the issue is and I may be able to get a fix together for 2.1.

Earlier, you didn't mention that refreshing the DB repeatedly was required, and as far as I can tell it is necessary to trigger the bug that I've found. Are you sure it is the same issue?
Aug 7 at 8:07 PM
Step ups in memory are caused pretty much by anything but in order of magnitude

1) If I hit the "refreshDB" button
2) If I run code on the python interactive
3) If I create a new python project


Please not that I am unable to launch a large Python solution directly from the .sln file. At most I can try to first open VS and then try to open the solution.

This doesn't happen with other languages or if I open the identical solution using maybe Eclipse.
Coordinator
Aug 7 at 8:26 PM
Does that last problem occur with the smaller database you sent me? There are no problems with that DB itself, so it may be unrelated - we've never done an excellent job once you have more than a few hundred files in your project or solution, but we shouldn't be that bad.
Aug 8 at 9:04 AM
Yes, it does. I am surprised other users didn't notice it.

For Large project I have a NULL REFERENCE EXCEPTION when I try to open them directly from the .sln file.
Aug 8 at 1:54 PM
I also noticed that the time required to open a project is significantly different when there is a complationDB or not.

a trivial and empty print('Hello World') project takes couple of minutes to open with memory going up to 2.GB before it is visible in the IDE when there is a completionDB (In my case I have 18 packages installed). the memory keeps going up up to 3.4GB and sometimes gets dumped back to 2.8 GB otherwise VS crashes.

Without completion DB I dont have any issues, therefore the way the objects related to the completionDB and the way are garbage collected is an issue.

I tried to change gc setting and I can see improvements but can you please have a look into this a bit more. Otherwise this tool is not useful for large scale projects and it is a shame.
Aug 8 at 2:25 PM
it would be great if you can look into the CompletionDB. (please look at the issue section same header). thanks!