Skip to content

Instantly share code, notes, and snippets.

@ExpHP
Created August 29, 2017 19:08
Show Gist options
  • Select an option

  • Save ExpHP/d77b83290b81da7874c75c090c6c2117 to your computer and use it in GitHub Desktop.

Select an option

Save ExpHP/d77b83290b81da7874c75c090c6c2117 to your computer and use it in GitHub Desktop.
numpy memory leak
from __future__ import print_function
import objgraph
import numpy as np
print('--------------------------------------------------')
print('Numpy info:')
print()
print(np.__version__)
print()
np.show_config()
print()
print('--------------------------------------------------')
def show_usage():
import resource
gb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 / 1024
print('max memory usage: {:.02f} GiB'.format(gb))
def show_growth():
print('========= new instances =========')
objgraph.show_growth()
print('=================================')
print('BEFORE')
show_usage()
show_growth()
print()
a = np.array([])
b = np.array([])
for _ in range(10**7):
np.dot(a, b)
print('AFTER')
show_usage()
show_growth()
$ python2 a.py
--------------------------------------------------
Numpy info:
1.14.0.dev0+0032e53
lapack_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
define_macros = [('HAVE_CBLAS', None)]
language = c
blas_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
define_macros = [('HAVE_CBLAS', None)]
language = c
openblas_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
define_macros = [('HAVE_CBLAS', None)]
language = c
blis_info:
NOT AVAILABLE
openblas_lapack_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
define_macros = [('HAVE_CBLAS', None)]
language = c
lapack_mkl_info:
NOT AVAILABLE
blas_mkl_info:
NOT AVAILABLE
--------------------------------------------------
BEFORE
max memory usage: 0.00 GiB
========= new instances =========
function 3373 +3373
wrapper_descriptor 2241 +2241
dict 937 +937
builtin_function_or_method 934 +934
tuple 890 +890
method_descriptor 824 +824
weakref 769 +769
getset_descriptor 576 +576
list 462 +462
type 311 +311
=================================
AFTER
max memory usage: 1.00 GiB
========= new instances =========
wrapper_descriptor 2250 +9
getset_descriptor 580 +4
member_descriptor 242 +3
weakref 771 +2
dict 939 +2
=================================
$ python3 a.py
--------------------------------------------------
Numpy info:
1.14.0.dev0+0032e53
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
openblas_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
language = c
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/lib64']
language = c
define_macros = [('HAVE_CBLAS', None)]
--------------------------------------------------
BEFORE
max memory usage: 0.03 GiB
========= new instances =========
function 4894 +4894
wrapper_descriptor 2033 +2033
dict 1971 +1971
tuple 1753 +1753
weakref 1264 +1264
method_descriptor 1035 +1035
builtin_function_or_method 949 +949
getset_descriptor 827 +827
list 579 +579
set 538 +538
=================================
AFTER
max memory usage: 1.09 GiB
========= new instances =========
=================================
$ uname -a
Linux arch-t430s 4.12.8-2-ARCH #1 SMP PREEMPT Fri Aug 18 14:08:02 UTC 2017 x86_64 GNU/Linux
$ python2 --version
Python 2.7.13
$ python3 --version
Python 3.6.2
$ ls -l /usr/lib64/libopenblas.so
lrwxrwxrwx 1 root root 35 Aug 9 12:50 /usr/lib64/libopenblas.so -> libopenblas_sandybridgep-r0.2.20.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment