在 python 中是否有某种方法可以手动释放为字符串分配的内存,而其他一些变量仍然引用它?我想在函数内部释放传递给函数的字符串。
def use_string(s):
do_something_with_s(s)
#can I free memory used for s here?
do_other_things_that_dont_need_s()
my_string = "hi there."*100000
use_string(my_string)
do_some_other_things_that_dont_need_my_string()
据我所知,s(在 use_string 的上下文中)和 my_string 都将引用相同的不可变字符串对象,只有在没有变量指向它时才能释放它。这是真的吗?
请您参考如下方法:
是的,这就是del keyword是为了。 用垃圾收集语言执行此操作是否真的有意义是另一回事。
