- Notifications
You must be signed in to change notification settings - Fork749
Closed
Description
Environment
- Pythonnet version: 3.0.0rc3
- Python version: 3.10.5
- Operating System: Windows 10 20H2
- .NET Runtime: 6.0.6
Details
- Describe what you were trying to get done.
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
{<class 'str'>: 0, <class 'System.String'>: 1000}
You can also visualize the memory consumption:
fig,ax=plt.subplots()ax.plot(memory_py,label='Python object')ax.plot(memory_net,label='DotNet object')ax.legend()plt.show(fig)
You can execute the same code with pythonnet 2.5.2 (Python 3.9.13) and get the following figure
Metadata
Metadata
Assignees
Labels
No labels