You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Deleted dotnet object does not get garbage collected #1872
Create dotnet-object and delete it. If I do that a lot I get memory issues. It seems the never get cleared by the Python- or dotnet-gc. This is not a problem in pythonnet 2.5.2
What commands did you run to trigger this issue?
importgcimportclrimportpsutilimportmatplotlib.pyplotaspltimportSystem# Create dict store count of objects lefttypes= [
str,
System.String,
]
types= {obj: 0forobjintypes}
N=1_000str_size=1_000_000memory_py= [0for_inrange(N+2)]
memory_py[0] =psutil.virtual_memory().usedforiiinrange(N):
s_py=str('a'*str_size)
dels_pymemory_py[ii+1] =psutil.virtual_memory().used-memory_py[0]
gc.collect()
# See what types of objects are still presentforobjingc.get_objects():
iftype(obj) inlist(types.keys()):
types[type(obj)] +=1print(types)
memory_py[ii+2] =psutil.virtual_memory().used-memory_py[0]
memory_py[0] =0
{<class 'str'>: 0, <class 'System.String'>: 0}
memory_net= [0for_inrange(N+2)]
memory_net[0] =psutil.virtual_memory().useds_py=str('a'*str_size)
foriiinrange(N):
s_net=System.String(s_py)
dels_netmemory_net[ii+1] =psutil.virtual_memory().used-memory_net[0]
gc.collect()
# See what types of objects are still presentforobjingc.get_objects():
iftype(obj) inlist(types.keys()):
types[type(obj)] +=1print(types)
memory_net[ii+2] =psutil.virtual_memory().used-memory_net[0]
memory_net[0] =0
Activity
lostmsu commentedon Jul 13, 2022
You are not calling .NET GC
m-rossi commentedon Jul 14, 2022
@lostmsu I get the same results if I add
System.GC.Collect()Full code for reference:
filmor commentedon Jul 14, 2022
Hmm, this tells us that Python is not releasing the wrapper objects for some reason. Can you check the refcount and referrers for these?
m-rossi commentedon Jul 14, 2022
I used objgraph to display the backrefs. The output of
gc.get_referrers(obj)is really huge or have you another advice to display the referrers?lostmsu commentedon Jul 14, 2022
@filmor an update from my debugging the issue:
From this code I am getting a refcount
rcof 3:Replacing
Uriwith Python'sstrgivesrc1.The issue does not seem to happen when objects are created from .NET. E.g. this does not produce extra references:
Looks like we leak references when constructing new .NET objects from Python.
fixed leak in NewReference.Move
fixed leak in NewReference.Move
fixed leak in NewReference.Move
filmor commentedon Jul 14, 2022
@lostmsu Great work, thanks! And thank you @m-rossi for testing and reporting! Could you verify that the newest
releasebranch indeed fixes the issue?m-rossi commentedon Jul 15, 2022
Looks good, the

gcsees noSystem.String-objects left and the memory consumption is better too:Thanks @lostmsu !
fixed leak in NewReference.Move