python逐行读取txt文件时堆栈溢出_如何在python中引起堆栈溢出和堆溢出

我试图理解python如何管理堆栈和堆。所以我想做一些“糟糕”的编程,导致堆栈溢出和堆溢出。我不明白的是,例如字符串为什么要去堆栈,而其他所有字符串都去堆。只是设计师的同意吗?这些例子正确吗?

据我所读,python中的所有内容都是在堆中生成的,因为它是面向对象的,对吧?

EDITED:我认为C等语言中的堆栈具有固定的长度,但在python中,甚至堆栈都是动态分配的,正如Anycorn在他的回答中所说。这就是为什么如果我同时尝试一个大字符串(在堆栈上)或一个列表(在堆上),我也会得到完整的内存。如果我错了,请纠正我。谢谢Memory management in Python involves a private heap containing all

Python objects and data structures. The management of this private

heap is ensured internally by the Python memory manager. The Python

memory manager has different components which deal with various

dynamic storage management aspects, like sharing, segmentation,

preallocation or caching.

At the lowest level, a raw memory allocator ensures that there is

enough room in the private heap for storing all Python-related data by

interacting with the memory manager of the operating system. On top of

the raw memory allocator, several object-specific allocators operate

on the same heap and implement distinct memory management policies

adapted to the peculiarities of every object type.

这里有一些例子。您可以复制粘贴到Python official visualizer中,但使用较小的值,因为它不会运行。。。

对于堆栈溢出:import time

word = "test "

x = word*1000000000

time.sleep(10)

print ("this message wont appear if stack overflow has occurred!")

我明白了x = word*1000000000

MemoryError

如果我删除一个零,它就会运行。我使用x = word*500000000时获得最大内存使用量

所以我不能使堆栈溢出,因为即使是堆栈也是动态分配的?

对于堆溢出:i = 10000

test_list = [0]

while i > 0 :

test_list [:0] = test_list #insert a copy of itself at the beginning

i -= 1

现在我不明白的是垃圾收集器是如何进入程序的,它是不是同时在堆栈和堆上运行,因为它们都是动态分配的?是因为O/S内存管理器吗?关于python编程语言的特性,这些东西告诉了我们什么?

这是否证明“动态语言”或“解释性语言”一词是合理的?

很抱歉问了这么长的问题,但我只是想澄清一下我的一些想法。

提前谢谢!

编辑

我找到了我要找的东西:

如果调用

sys.setrecursionlimit(N)值大于系统实际可以处理的N,然后尝试递归到该深度。在某个时候,系统将耗尽堆栈空间,Python解释器将崩溃。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值