Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
# примеры для Python 2.7
def create_temp_strings():
temp_value = [str(x) for x in xrange(10000000)]
def create_temp_ints():
temp_value = [x*2 for x in xrange(10000000)]
Space for integer objects in particular lives in an immortal free list of unbounded size… If you don't want that, don't do that ;-)
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: /* Integers are quite normal objects, to make object handling uniform.
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: (Using odd pointers to represent integers would save much space
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: but require extra checks for this special case throughout the code.)
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000: Since a typical Python program spends much of its time allocating
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: and deallocating integers, these operations should be very fast.
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: Therefore we use a dedicated allocation scheme with a much lower
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: overhead (in space and time) than straight malloc(): a simple
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: dedicated free list, filled when necessary with memory from malloc().
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000:
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000: block_list is a singly-linked list of all PyIntBlocks ever allocated,
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000: linked via their next members. PyIntBlocks are never returned to the
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000: system before shutdown (PyInt_Fini).
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000:
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000: free_list is a singly-linked list of available PyIntObjects, linked
tim 01478a132908 Sun Apr 28 16:57:34 2002 +0000: via abuse of their ob_type members.
guido a6934380c6e7 Thu Dec 20 15:06:42 1990 +0000: */
/* This is the main function. Read this to understand how the
* collection process works. */
static Py_ssize_t
collect(int generation)
{
…
здесь много кода
...
/* Clear free list only during the collection of the highest
* generation */
if (generation == NUM_GENERATIONS-1) {
clear_freelists();
}
/* Clear all free lists
* All free lists are cleared during the collection of the highest generation.
* Allocated items in the free list may keep a pymalloc arena occupied.
* Clearing the free lists may give back memory to the OS earlier.
*/
static void
clear_freelists(void)
{
(void)PyMethod_ClearFreeList();
(void)PyFrame_ClearFreeList();
(void)PyCFunction_ClearFreeList();
(void)PyTuple_ClearFreeList();
#ifdef Py_USING_UNICODE
(void)PyUnicode_ClearFreeList();
#endif
(void)PyInt_ClearFreeList();
(void)PyFloat_ClearFreeList();
}
Filename: tests/test_good.py
Line # Mem usage Increment Line Contents
================================================
6 @profile
7 5.785 MB 0.000 MB def func():
8 21.117 MB 15.332 MB a = range(10**6)
9 17.301 MB -3.816 MB del a
10 5.895 MB -11.406 MB ctypes.pythonapi.PyInt_ClearFreeList()
11 5.895 MB 0.000 MB gc.collect(2)
Filename: tests/test_bad.py
Line # Mem usage Increment Line Contents
================================================
6 @profile
7 5.781 MB 0.000 MB def func():
8 21.117 MB 15.336 MB a = range(10**6)
9 21.117 MB 0.000 MB b = int('300')
10 17.301 MB -3.816 MB del a
11 17.309 MB 0.008 MB ctypes.pythonapi.PyInt_ClearFreeList()
12 17.309 MB 0.000 MB del b
13 5.895 MB -11.414 MB ctypes.pythonapi.PyInt_ClearFreeList()
14 5.895 MB 0.000 MB gc.collect(2)
Filename: tests/test_bad_hack.py
Line # Mem usage Increment Line Contents
================================================
6 @profile
7 5.789 MB 0.000 MB def func():
8 21.117 MB 15.328 MB a = range(10**6)
9 21.117 MB 0.000 MB b = int('300')
10 17.301 MB -3.816 MB del a
11 17.309 MB 0.008 MB ctypes.pythonapi.PyInt_ClearFreeList()
12 5.785 MB -11.523 MB libc.malloc_trim(0)
13 #del b
14 5.785 MB 0.000 MB ctypes.pythonapi.PyInt_ClearFreeList()
15 5.785 MB 0.000 MB gc.collect(2)
Filename: tests/test_ugly.py
Line # Mem usage Increment Line Contents
================================================
6 @profile
7 5.781 MB 0.000 MB def func():
8 5.781 MB 0.000 MB i = 0
9 5.781 MB 0.000 MB a = []
10 13.094 MB 7.312 MB while i < 10**5: # 10**5 чтобы было быстрее
11 13.094 MB 0.000 MB a.append(i)
12 13.094 MB 0.000 MB i += 1
13 12.711 MB -0.383 MB del a
14 12.711 MB 0.000 MB del i
15 12.719 MB 0.008 MB ctypes.pythonapi.PyInt_ClearFreeList()
16 12.711 MB -0.008 MB libc.malloc_trim(0)
17 12.711 MB 0.000 MB ctypes.pythonapi.PyInt_ClearFreeList()
18 12.711 MB 0.000 MB gc.collect(2)
libc = ctypes.CDLL('libc.so.6')
import ctypes
import gc
import time
PAUSE = 1.0
def func():
time.sleep(PAUSE)
i = 0
time.sleep(PAUSE)
a = []
time.sleep(PAUSE)
while i < 10**5:
a.append(i)
i += 1
time.sleep(PAUSE)
del a
time.sleep(PAUSE)
del i
time.sleep(PAUSE)
ctypes.pythonapi.PyInt_ClearFreeList()
time.sleep(PAUSE)
gc.collect(2)
time.sleep(PAUSE)
if __name__ == '__main__':
func()
import psutil
import sys
import time
def main():
proc = psutil.Popen([sys.executable, 'test_ugly.py'])
stats = []
while proc.poll() is None:
# target is alive
stats.append((time.time(), proc.get_memory_info().vms / 1024.0 / 1024))
time.sleep(0.1)
with open('result.txt', 'w') as out:
out.write('%s\n' % '\n'.join('%s %.2f' % stat for stat in stats))
if __name__ == '__main__':
main()

Использование памяти в Python