Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak loading python modules compiled by MinGW #271

Open
buergi opened this issue Feb 23, 2015 · 15 comments
Open

Memory leak loading python modules compiled by MinGW #271

buergi opened this issue Feb 23, 2015 · 15 comments
Labels

Comments

@buergi
Copy link

buergi commented Feb 23, 2015

Just tried to build an own python module with the MinGW gcc shipped with anaconda.
When loading the build module python allocates >4GB memory instantly, freezing the whole computer and freeing it not before python is closed.
I use a fresh installation of Anaconda3 2.1.0 for Win7 x64 with python 3.4 and the following useless python module.
foo.c

#include <Python.h>
static PyMethodDef funcs[] = {
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef foomodule = {
    PyModuleDef_HEAD_INIT,"foo","just foo",-1,   funcs };

PyMODINIT_FUNC PyInit_foo(void) {
    return PyModule_Create(&foomodule);
}

setup.py

from distutils.core import setup, Extension
setup (name = 'foo', version = '1.0',description = 'foo',
       author = 'me', ext_modules = [Extension('foo', sources = ['foo.c'])])

Calling python setup.py build and importing the module yields the undesired result.

I guess it has something to do with libpython, is there anything I can do to produce working python modules?

@buergi
Copy link
Author

buergi commented Feb 23, 2015

I reproduced the problem on Windows 8.1 and I did some further tests. It seems importlib._bootstrap allocates exactly 4GB for some reason:

Python 3.4.1 |Anaconda 2.1.0 (64-bit)| (default, Sep 24 2014, 18:32:42) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tracemalloc
>>> tracemalloc.start(25)
>>> import foo
>>> s = tracemalloc.take_snapshot()
>>> t = s.statistics('lineno')
>>> print(t[0])
<frozen importlib._bootstrap>:321: size=4096 MiB, count=5, average=819 MiB
>>> t = s.statistics('traceback')
>>> for i in t[0].traceback.format(): print(i)
...
  File "<frozen importlib._bootstrap>", line 321
  File "<frozen importlib._bootstrap>", line 1715
  File "<frozen importlib._bootstrap>", line 539
  File "<frozen importlib._bootstrap>", line 1161
  File "<frozen importlib._bootstrap>", line 1191
  File "<frozen importlib._bootstrap>", line 2226
  File "<frozen importlib._bootstrap>", line 2237
  File "<stdin>", line 1

@buergi
Copy link
Author

buergi commented Feb 24, 2015

The problem is buried deep in the python internal import routines, as it also occurs when using _imp:

import _imp
_imp.load_dynamic("foo","foo.pyd")

So I guess it has something to do with mingw's binary format which python does not like.
I did not manage to get the MS x64 compiler working so, but I guess it would work with it.
Furthermore I can confirm that it is working in the 32-bit Version of anaconda.

@buergi
Copy link
Author

buergi commented Feb 24, 2015

So finally I got it working, compiling with Windows SDK 7.1 for x86 and x64 both imports fine, so it really is a pure MinGW x64 problem.

@ccordoba12
Copy link

@buergi it's known that MinGW x64 is still unstable. It should be recommended to use Visual Studio in Anaconda documentation.

@ccordoba12
Copy link

@ilanschnell what do you think about suggesting in our docs that Visual Studio should be used to compile extensions on Windows?

@ilanschnell
Copy link
Contributor

Sure, but this is general Python knowledge, not specific to Anaconda.

@ccordoba12
Copy link

@ilanschnell yes, but since Anaconda comes with MinGW, people assumes is better to use it instead of Visual Studio.

@ccordoba12
Copy link

For example, see issue #175

@carlkl
Copy link

carlkl commented Mar 2, 2015

@buergi 's test module works out of the box with the Winpython distribution https://winpython.github.io. This distro is equiped with the mingwstatic toolchain https://bitbucket.org/carlkl/mingw-w64-for-python/downloads that is in development particularly for compilation of python extensions.
https://bitbucket.org/carlkl/mingw-w64-for-python/downloads/mingwpy-2015-01-readme.html gives an explanation on how to use this toolchain.

@ccordoba12
Copy link

Pinging @stonebig to see if he can give us further context.

@carlkl
Copy link

carlkl commented Mar 2, 2015

the mingwstatic toolchains mentioned have some enhancements for better compatibility to the MSVC compilers used by the Windows Python distributions. It should be noted, that it is possible to compile numpy and scipy for 32 and 64 bit with this toolchains. Several issues with some problematic scipy testcases are not solved yet.
However, compiling cython extensions should work out of the box though. Think of this toolchains as a convinient gcc based build system for python extensions.

@ccordoba12
Copy link

@carlkl thanks for the explanation. But it'd be better to have a full fledged compiler as part of Anaconda. What about tdm-gcc? (mentioned in issue #175 )

@carlkl
Copy link

carlkl commented Mar 2, 2015

the mingwstatic toolchain IS a fully fledged compiler toolchain with its own set of patches. tdm-mingw i.e. doesn't solve the CRT mismatch issues your are faced with development of python extensions on Windows.

@stonebig
Copy link

stonebig commented Mar 2, 2015

Hi all,

Carl said it all.

I do think the carlkl mingwstatic toolchain is the pertinent cython companion for windows, because it solves all problems:
. it's for whatever version of python 2.7/3.3/3.4, 32bit to 64bit,
. it's more stable than anything else because dll are static,
. openMP is included,
. you can easily include it in your distribution,
....

I don't think I received any complaint since I ship carlkl mingwstatic in Winpython, since 4 months

Hope it helps

@ccordoba12
Copy link

@stonebig, thanks a lot for the clarification!!

@carlkl, sorry for the confusion. I thought mingwstatic was not a full complier :-)

ysig added a commit to ysig/GraKeL that referenced this issue Jan 3, 2018
MinGW gcc when used upon python build creates a 4 GB overhead upon import. Try with visual c++. Possibly won't work like that. For the error see ContinuumIO/anaconda-issues#271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants