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 with CTypes Structure #57207

Closed
a01 mannequin opened this issue Sep 17, 2011 · 6 comments
Closed

Memory leak with CTypes Structure #57207

a01 mannequin opened this issue Sep 17, 2011 · 6 comments
Labels
performance Performance or resource usage topic-ctypes

Comments

@a01
Copy link
Mannequin

a01 mannequin commented Sep 17, 2011

BPO 12998
Nosy @alex, @skrah

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2011-11-03.18:09:57.570>
created_at = <Date 2011-09-17.06:29:26.090>
labels = ['ctypes', 'performance']
title = 'Memory leak with CTypes Structure'
updated_at = <Date 2011-11-03.18:09:57.570>
user = 'https://bugs.python.org/a01'

bugs.python.org fields:

activity = <Date 2011-11-03.18:09:57.570>
actor = 'skrah'
assignee = 'none'
closed = True
closed_date = <Date 2011-11-03.18:09:57.570>
closer = 'skrah'
components = ['ctypes']
creation = <Date 2011-09-17.06:29:26.090>
creator = 'a01'
dependencies = []
files = []
hgrepos = []
issue_num = 12998
keywords = []
message_count = 6.0
messages = ['144170', '144171', '144172', '144173', '144178', '144179']
nosy_count = 4.0
nosy_names = ['lunaryorn', 'alex', 'skrah', 'a01']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'resource usage'
url = 'https://bugs.python.org/issue12998'
versions = []

@a01
Copy link
Mannequin Author

a01 mannequin commented Sep 17, 2011

When using Python 2.5.4 on Windows (and potentially other versions and platforms), usage of CTypes like the following cause a memory leak. The memory leak can be verified with ProcessExplorer, as the memory Python takes up keeps increasing until eventually Python will get a MemoryError.
----

import ctypes
def hi():
    class c1(ctypes.Structure):
            _fields_=[('f1',ctypes.c_uint8)]
    class c2(ctypes.Structure):
            _fields_=[('g1',c1*2)]

while True:
    test=hi()

This is true even if the garbage collector is called explicitly:
----

import gc
import ctypes
def hi():
    class c1(ctypes.Structure):
            _fields_=[('f1',ctypes.c_uint8)]
    class c2(ctypes.Structure):
            _fields_=[('g1',c1*2)]

while True:
    test=hi()
    test2=gc.collect()

It is also true if "del" is called on the return value of the function.

In order to get the memory leak, it appears to be necessary to have two Structure classes, with one of them containing a "multiple" of the other one as a field.

In a code project where functions returning Structure classes similarly to this example are being used, MemoryError has been encountered.

See: http://stackoverflow.com/users/553702/user553702

@a01 a01 mannequin added topic-ctypes performance Performance or resource usage labels Sep 17, 2011
@a01
Copy link
Mannequin Author

a01 mannequin commented Sep 17, 2011

@alex
Copy link
Member

alex commented Sep 17, 2011

This is caused by a cache which is kept of array's for different (Structure, length) pairs.

@a01
Copy link
Mannequin Author

a01 mannequin commented Sep 17, 2011

If it is a cache, shouldn't it be garbage-collected or limited in size? Why does it grow without bounds?

@lunaryorn
Copy link
Mannequin

lunaryorn mannequin commented Sep 17, 2011

Why should it? After all, you're sort of abusing ctypes by repeatedly creating Struture types over and over again. C structures that you might want to wrap with these types are fixed and known at the time of programming, so there is never a need to create the same Structure type twice. Thus the set of Structure subclasses created during the live-time of a program is both, fixed and small, so there is no need to limit the cache size.

@skrah
Copy link
Mannequin

skrah mannequin commented Sep 17, 2011

I can reproduce the leak with Python 2.5.4, but not with Python 2.6.5
or Python 3.2.

Python 2.5.4 is an ancient version. Please upgrade to Python 2.7
or Python 3.2. If the leak still exists, just respond to this issue
and it will be opened again automatically.

@skrah skrah mannequin closed this as completed Nov 3, 2011
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage topic-ctypes
Projects
None yet
Development

No branches or pull requests

1 participant