The following code uses multiprocessing Array to share a large array of unicode strings for all processes. If I use c_wchar_p as a type, memory usage for the child process is about a quarter of the memory used in the parent process (the amount changes if I change the number of records in the array).
However, if I use ctypes.Structure with one c_wchar_p field, the memory usage for the child process is constant and very low, while the memory usage of the parent process is doubled.
import ctypes import multiprocessing import random import resource import time a = None class Record(ctypes.Structure): _fields_ = [('value', ctypes.c_wchar_p)] def __init__(self, value): self.value = value def __str__(self): return '(%s)' % (self.value,) def child(i): while True: print "%ik memory used in child %i: %s" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, i, a[i]) time.sleep(1) for j in xrange(len(a)): c = a[j] def main(): global a
Using c_wchar_p leads to this conclusion:
363224k memory used in parent: unicode 0! 72560k memory used in child 5: unicode 5! 72556k memory used in child 3: unicode 3! 72536k memory used in child 1: unicode 1! 72568k memory used in child 4: unicode 4! 72576k memory used in child 2: unicode 2!
Using the recording results on this output:
712508k memory used in parent: (unicode 0!) 1912k memory used in child 1: (unicode 1!) 1908k memory used in child 2: (unicode 2!) 1904k memory used in child 5: (unicode 5!) 1904k memory used in child 4: (unicode 4!) 1908k memory used in child 3: (unicode 3!)
Why?
papercrane
source share