Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Python garbage collection on a running script

In order to investigate some issues with unreleased system resources I'd like to force immediate garbage collection on an already running python script.

Is this somehow possible, e.g. by sending some kind of signal that Python would understand as an order to run gc; or any other similar way? Thanks.

I'm running Python 2.7 on a Linux server.

like image 901
knaperek Avatar asked Oct 30 '22 08:10

knaperek


1 Answers

Install a signal handler using the built-in signal module, then call gc.collect() from your custom handler.

If you have no control over the application, you'll have to use something like pyringe, which will allow you to insert arbitrary python.

like image 59
TkTech Avatar answered Nov 14 '22 10:11

TkTech