windows7上的简单循环会造成内存泄漏吗?

时间:2022-09-01 10:23:02

I am unsure about this, but this question might be related to Memory leak in batch for loop?.

我对此不确定,但这个问题可能与批量循环中的内存泄漏有关。

I have the following trivial batch script:

我有以下简单的批处理脚本:

@echo off
:label
cmd.exe /c cls
echo hello
goto label

When this script runs on a Windows7 32-bit installation, it behaves well. But when I run it on the few Windows7 64-bit machines that I have access to, it slowly and apparently irrevocably eats up all system memory - as seen in the Windows task manager. The memory stays in use after closing the window that executes the above batch lines.

当此脚本在Windows7 32位安装上运行时,它表现良好。但是,当我在我可以访问的几台Windows7 64位计算机上运行它时,它慢慢地显然不可挽回地占用了所有系统内存 - 正如Windows任务管理器中所见。关闭执行上述批处理行的窗口后,内存保持使用状态。

Is this happening to other people? If not, do you know what could cause this? Other installed software on the system?

这发生在其他人身上吗?如果没有,你知道是什么原因引起的吗?系统上安装的其他软件?

I have the same behavior if I do a

如果我这样做,我会有同样的行为

while True: os.system('cls')

loop in a Python-X/Y shell. It seems to be linked to the creation of a sub process. If I change the above from cmd.exe /c cls to just cls, I do not get a memory leak. I ran into this problem because I ported a Python script from Linux that repeatedly does a clear screen for a statistics display, implemented for Windows as shown in Clear the screen in python.

循环在Python-X / Y shell中。它似乎与创建子流程有关。如果我将上面的内容从cmd.exe / c cls更改为只是cls,我就不会出现内存泄漏。我遇到了这个问题,因为我从Linux移植了一个Python脚本,它反复为统计显示屏幕显示,为Windows实现,如清除python中的屏幕所示。

1 个解决方案

#1


0  

I ran it on my x64 machine and it works ok, it does start to struggle as time goes but I wouldn't call it a memory leak, if anything it just hammers the CPU when it spikes.

我在我的x64机器上运行它并且它工作正常,它确实开始挣扎随着时间的推移但我不会把它称为内存泄漏,如果有什么它只是在它尖峰时锤击CPU。

It could just be struggling to keep up with the commands depending on the performance of the computer. I added in a timeout for a second, now cmd is much happier and showing less than 1MB RAM consumption.

根据计算机的性能,它可能只是在努力跟上命令。我在超时中加入了一秒钟,现在cmd更加快乐并且显示内存消耗不到1MB。

Try just adjusting this to your needs, or find middle ground where you get the speed and the performance, just give cmd a break :)

尝试根据您的需要进行调整,或找到获得速度和性能的中间位置,只需给cmd一个休息:)

@echo off
:label
cmd.exe /c cls
echo hello
timeout /t 1 >nul
goto label

#1


0  

I ran it on my x64 machine and it works ok, it does start to struggle as time goes but I wouldn't call it a memory leak, if anything it just hammers the CPU when it spikes.

我在我的x64机器上运行它并且它工作正常,它确实开始挣扎随着时间的推移但我不会把它称为内存泄漏,如果有什么它只是在它尖峰时锤击CPU。

It could just be struggling to keep up with the commands depending on the performance of the computer. I added in a timeout for a second, now cmd is much happier and showing less than 1MB RAM consumption.

根据计算机的性能,它可能只是在努力跟上命令。我在超时中加入了一秒钟,现在cmd更加快乐并且显示内存消耗不到1MB。

Try just adjusting this to your needs, or find middle ground where you get the speed and the performance, just give cmd a break :)

尝试根据您的需要进行调整,或找到获得速度和性能的中间位置,只需给cmd一个休息:)

@echo off
:label
cmd.exe /c cls
echo hello
timeout /t 1 >nul
goto label