简体   繁体   English

在带有和不带有':'运算符的python中删除列表

[英]Deletion of a list in python with and without ':' operator

I've been working with python for quite a bit of time and I'm confused regarding few issues in the areas of Garbage Collection, memory management as well as the real deal with the deletion of the variables and freeing memory. 我已经使用python了很长时间,对于垃圾收集,内存管理以及删除变量和释放内存的实际处理方面的几个问题,我感到困惑。

>>> pop = range(1000)
>>> p = pop[100:700]
>>> del pop[:]
>>> pop
[]
>>> p
[100.. ,200.. 300...699]

In the above piece of code, this happens. 在上面的代码中,这发生了。 But, 但,

>>> pop = range(1000)
>>> k = pop   
>>> del pop[:]
>>> pop 
[]
>>> k
[]

Here in the 2nd case, it implies that the k is just pointing the list 'pop'. 在第二种情况下,这意味着k只是指向列表“ pop”。

First Part of the question : 问题的第一部分:

But, what's happening in the 1st code block? 但是,第一代码块中发生了什么? Is the memory containing [100:700] elements not getting deleted or is it duplicated when list 'p' is created? 创建[p]列表时,包含[100:700]元素的内存是否未被删除?

Second Part of the question : 问题的第二部分:

Also, I've tried including gc.enable and gc.collect statements in between wherever possible but there's no change in the memory utilization in both the codes. 另外,我尝试了尽可能在两者之间包括gc.enable和gc.collect语句,但是两个代码中的内存利用率都没有变化。 This is kind of puzzling. 这有点令人困惑。 Isn't this bad that python is not returning free memory back to OS? python不将可用内存返回给操作系统,这不是很糟糕吗? Correct me if I'm wrong in the little research I've did. 如果我所做的少量研究不对,请纠正我。 Thanks in advance. 提前致谢。

Slicing a sequence results in a new sequence, with a shallow copy of the appropriate elements. 切片序列会生成一个新序列,并带有适当元素的浅表副本。

Returning the memory to the OS might be bad, since the script may turn around and create new objects, at which point Python would have to request the memory from the OS again. 将内存返回给操作系统可能很不好,因为脚本可能会转过来并创建新的对象,这时Python将不得不再次向操作系统请求内存。

1st part: 第一部分:

In the 1st code block, you create a new object where the elements of the old one are copied before deleting that one. 在第一个代码块中,创建一个新对象,在删除旧对象之前复制旧对象的元素。

In the 2nd code block, however, you just assign a reference to the same object to another variable. 但是,在第二个代码块中,您只是将对同一对象的引用分配给另一个变量。 Then you empty the list, which, of course, is visible via both references. 然后,清空列表,当然,这两个引用都可见。

2nd part: Memory is returned when appropriate, but not always. 第二部分:适当时返回内存,但并非总是如此。 Under the hood of Python, there is a memory allocator which has control over where the memory comes from. 在Python的内部,有一个内存分配器,可以控制内存的来源。 There are 2 ways: via the brk()/sbrk() mechanism (for smaller memory blocks) and via mmap() (larger blocks). 有两种方法:通过brk()/sbrk()机制(用于较小的内存块)和通过mmap() (用于较大的块)。

Here we have rather smaller blocks which get allocated directly at the end of the data segment: 这里我们有一些较小的块,它们直接在数据段的末尾分配:

datadatadata object1object1 object2object2

If we only free object1, we have a memory gap which can be reused for the next object, but cannot easily freed and returned to the OS. 如果仅释放object1,则存在一个内存缺口,可以将其用于下一个对象,但无法轻松释放并返回给OS。

If we free both objects, memory could be returned. 如果我们释放两个对象,则可能会返回内存。 But there probably is a threshold for keeping memory back for a while, because returning everything immediately is not the very best thing. 但是可能有一个将内存保留一段时间的阈值,因为立即返回所有内容并不是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM