在一批循环内存泄漏? [英] Memory leak in batch for loop?
问题描述
我写一个批处理脚本,通过每一个文件去在找code文件目录下,并修改它们以某种方式。我完成了任务,我试图约6000文件放在一个大的目录中运行它。在脚本中约40分钟死机了,我得到了很多内存不足的错误从命令提示符,运行脚本,而看任务管理器发现,我的计划是为每循环迭代1MB左右的速度吃内存。所以很自然想我做错了什么我切出一切,我曾写信给隔离问题code。但后来我留下了,除了一个for循环,问题仍然存在一个空文件!
I am writing a batch script that goes through every file in a directory looking for code files and modifies them in some way. After I finished that task I tried to run it on a large directory with about 6,000 files. About 40 minutes in the script crashed and I got lots of out of memory errors from the command prompt, running the script while looking at the task manager showed that my program was eating memory at about a rate of 1MB per loop iteration. So naturally thinking I had done something wrong I cut out all the code that I had written to isolate the problem. But then I was left with an empty file except for a for loop and the problem still persisted!
下面是我跑了一个相当大的目录就像我说的:
Here is what I ran on a fairly large directory like I said:
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set directory=%CD%
for /R "%directory%" %%a in (*.c *.cpp *.h *.idl) do (
set currentDir=%%~dpa
pushd !currentDir!
popd
)
其实我也能修剪下来:
I have actually been able to trim it down to:
@echo off
for /R "%CD%" %%a in (*) do echo
和问题仍然存在。
里还有一批循环内存泄漏或我只是做错了什么?
Is there a memory leak in the batch for loop or am I just doing something wrong?
我运行Windows XP 32位Service Pack 2的虽然我已经测试并确认问题依然美元的Service Pack 3。
I am running Windows XP 32bit Service Pack 2 although I have tested and confirmed the problem is still present in Service Pack 3.
推荐答案
这是没有这么多的答案作为一种解决方法(或重写)。我已经运行脚本,我看到一些内存消耗,这其中有些是不释放,直到脚本结束,但我不靠近每个迭代1 MB得到任何东西。 (就像我@aphoria可以运行在C盘根的脚本:不会对XP SP3和Vista的问题)
This is not so much an answer as a workaround (or rewrite). I have run your script and I see some memory consumption, which some of which is not released until the script finishes, but I do not get anything near 1 MB per iteration. (Just like @aphoria I can run the script on the root of c: without problems on XP SP3 and Vista.)
我建议你关闭任何进程没有必要为脚本,在一个时间上的一个子目录中运行你的脚本。
I suggest that you closing any process not necessary for the script and run your script on one subdirectory at a time.
如果你解决不了这个以任何其他方式,我建议你尝试写在PowerShell中的脚本。
If you cannot solve this in any other manner I suggest you try writing the script in Powershell.
这篇关于在一批循环内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!