Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in batch for loop?

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

And the problem still persists.

Is there a memory leak in the batch for loop or am I just doing something wrong?

I am running Windows XP 32bit Service Pack 2 although I have tested and confirmed the problem is still present in Service Pack 3.

like image 650
Maynza Avatar asked Jun 13 '11 12:06

Maynza


1 Answers

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.

If you cannot solve this in any other manner I suggest you try writing the script in Powershell.

like image 108
steenhulthin Avatar answered Nov 15 '22 17:11

steenhulthin