该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

我重复一楼的问题, range(1024*1024)确实占用很大内存,但是del后,内存几乎是马上就释放了, 没有内存持续占用问题。我测试操作系统是mac ox 10.5.6

在实际应用中,range 对大的数是不适合的,应该用xrange。

可以参考:

http://avinashv.net/2008/05/pythons-range-and-xrange/

Original text: ange and xrange are very different in Python; do you use them correctly? I see a lot of code where the author seemingly didn’t know the difference. At first glance, there doesn’t seem to be one at all—in fact, when I started hacking in Python, I used them interchangeably because I couldn’t figure out what the difference was, and in all the code I was writing at the time, swapping one for the other made no conceivable difference.

It’s a subtle difference. range returns exactly what you think: a list of consecutive integers, of a defined length beginning with 0. xrange, however, returns an “xrange object”, which acts a great deal like an iterator. If you’ve spent anytime with iterators, this difference should make sense. Here’s an example:

range(1000000)

xrange(1000000)

range(1000000) will return a list of one million integers, whereas xrange(1000000) will return (what is essentially) an iterator sequence. Indeed, xrange supports iteration, whereas range does not. The overall benefit is minimal, because xrange (in the words of the Python manual) “still has to create the values when asked for them,” but at each call, it consumes the same amount of memory regardless of the size of the requested list. At extremely large values, this is a major benefit over range. Another benefit is also apparent: if your code is going to break out while traversing over a generated list, then xrange is the better choice as you are going to consume less memory overall if you break.

python 内存释放gc_python 内存释放问题,高手请帮帮忙相关推荐

  1. c语言打不开h文件,说那个“mem.h”头文件打不开 怎么改啊 高手们帮帮忙

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include "stdio.h" #include "stdlib.h" #include "str ...

  2. mem.h是C语言头文件吗,说那个“mem.h”头文件打不开 怎么改啊 高手们帮帮忙

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include "stdio.h" #include "stdlib.h" #include "str ...

  3. 当退出python时是否释放全部内存_Python面试题:高级特性考察

    1.函数装饰器有什么作用?请列举说明? 答: 装饰器就是一个函数,它可以在不需要做任何代码变动的前提下给一个函数增加额外功能,启动装饰的效果. 它经常用于有切面需求的场景,比如:插入日志.性能测试.事 ...

  4. 当退出python时是否释放全部内存_python如何释放内存

    关于Python中的内存释放问题 首先就不得不提到Python解释器在何种情况下会释放变量的内存.Python引用了内存计数这一简单的计数来控制. python学习网,大量的免费python视频教程, ...

  5. 当退出python时是否释放全部内存_python 关于高级特性的问题

    1.函数装饰器有什么作用?请列举说明? 2. Python 垃圾回收机制? 3. 魔法函数 _call_怎么使用? 4. 如何判断一个对象是函数还是方法? 5. @classmethod 和 @sta ...

  6. python释放变量内存_看完2019年阿里巴巴Python面试题详解,月薪3万不是梦

    很多人想自学Python找工作,下面给大家分享一部分阿里巴巴的Python开发工程师的面试题目: 概念理解类题目: 1.请说一下你对迭代器和生成器的区别? 答:(1)迭代器是一个更抽象的概念,任何对象 ...

  7. python强制释放内存_强制Python释放对象以释放内存

    我运行以下代码:from myUtilities import myObject for year in range(2006,2015): front = 'D:\\newFilings\\' ba ...

  8. python释放变量内存_2020Python面试题:Python是如何进行内存管理的?

    Python面试,Python面试题 一.垃圾回收: Python不像C++,Java等语言一样,他们可以不用事先声明变量类型而直接对变量进行赋值.对Python语言来讲,对象的类型和内存都是在运行时 ...

  9. python查看哪些内存被释放_python内存不释放原理 | shell's home

    在maillist里面看到无数次的有人问,python速度为什么这么慢,python内存管理很差.实话说,我前面已经说过了.如果你在意内存/CPU,不要用python,改用C吧.就算C不行,起码也用个 ...

最新文章

  1. 如何启用计算机上的无线网络连接,告诉大家win10无线网络连接如何打开
  2. mysql dba系统学习(19)配置mysql+lvs+keeplived实现Mysql读操作的负载均衡
  3. okhttp请求文件异常解决方法
  4. EGLImage与纹理
  5. 指尖时刻:百度移动营销生态体系
  6. 岛屿类-网格类问题-DFS | 力扣695. 岛屿的最大面积
  7. C++继承中的对象模型
  8. hpuoj--1093: 回文数(一)
  9. 前端开发工程化探讨--基础篇(长文)
  10. Hollowjars,部署扫描程序以及Wildfly群体为何很棒
  11. C#递归、动态规划计算斐波那契数列
  12. CentOS中配置lvm存储
  13. 每天一道剑指offer-链表中第k个节点
  14. 呼叫中心系统建设方案
  15. Python 面向对象 计算长方体、四棱锥的表面积和体积
  16. 射频信号发生器的使用以及相关中心频率,载波,调制波术语解释
  17. 企业微信机器人读取服务器,用企业微信机器人搞事情
  18. 免费网络硬盘、FTP、大容量邮箱、电子相册合集
  19. 如何使用Box2D和Cocos2D制作一款像Fruit Ninja一样的游戏-第3部分
  20. linux添加javahome

热门文章

  1. 什么是标签传播算法?为什么要使用标签传播算法?如何使用?
  2. pandas分层索引(层级索引、MultiIndex)的创建、取值、切片、统计计算以及普通索引和层级索引的转换方法
  3. java连接Hbase数据库
  4. python使用heapq快速查找最大或最小的 N 个元素
  5. Linux rm过滤后的目录6,Linux的部分命令
  6. 试编写一个汇编语言程序,要求对键盘输入的小写字母用大写字母显示出来
  7. 初中教师资格证计算机试讲教案模板,初中数学教师资格证面试教案模板: 《投影》...
  8. linux输入ls命令报错,Linux命令基础2-ls命令
  9. STM32使用GPIO_WriteBit()函数使LED灯闪烁
  10. python 检测文件或文件夹是否存在