如何在python中删除类的实例(强制)?

Sha*_*ane 2 python mechanize mechanize-python

我正在使用脚本来测试网站是否运行顺畅,基本上我每20分钟左右打开一次网站并检查响应时间等等.像这样:

while True:

    MechBrowser = mechanize.Browser()
    Response = MechBrowser.open("http://example.com")
    time.sleep(1000)
Run Code Online (Sandbox Code Playgroud)

我知道python本身会进行垃圾收集,我们真的不应该打扰,但是当我检查网络监视器时,我总会找到几个未关闭的连接,每个连接运行1小时或更长时间.并非所有打开的连接都会挂在那里,只有其中一些.我很困惑,或者有一种方法可以手动销毁这些实例?

Kei*_*ith 10

尝试关闭响应对象.


ism*_*ail 6

del手动对象,注意这不会删除对象,只会减少reference count对象的数量.当reference count一个对象到达zero垃圾收集器时,将其从内存中删除.