> Does ctypes, when using restype, frees allocated memory? > > For example, will the memory allocated by "strdup" be freed after the "del" > statement? If not, how can I free it?
I've tried the following program and I'm more confused now :) Can anyone explain the output? --- import ctypes import gc import resource def mem_usage(): return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss libc = ctypes.cdll.LoadLibrary('libc.so.6') strdup = libc.strdup strdup.argtypes = [ctypes.c_char_p] strdup.restype = ctypes.c_char_p size = 1 << 20 print(f'size: {size:,}') data = b'x' * size # 1MB mb = mem_usage() print(f'memory before: {mb:,}') n = 1000 print(f'n: {n:,}') for _ in range(n): strdup(data) gc.collect() ma = mem_usage() diff = ma - mb print(f'memory after: {ma:,}') print(f'diff: {diff:,}') print(f'diff/size: {diff/size:.2f}') --- Which prints --- size: 1,048,576 memory before: 21,556 n: 1,000 memory after: 1,035,180 diff: 1,013,624 diff/size: 0.97 --- -- https://mail.python.org/mailman/listinfo/python-list