0

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.

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.

Community
  • 1
  • 1
onno
  • 11
  • 2

1 Answers1

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.

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.

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

@echo off
:label
cmd.exe /c cls
echo hello
timeout /t 1 >nul
goto label
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Thanks for doing that test! You said it starts to struggle - did you see whether the memory use slowly creeps up in the task manager? – onno Jul 04 '12 at 02:42
  • @onno Yeah, after leaving it for a while in the constant loop the memory does start to creep up, not as much as the CPU which goes up quicker though. – Bali C Jul 04 '12 at 08:29
  • Thank you! That's the interesting part. Does the memory use go down again after you closed the cmd.exe window? For me it does not, and I can make the OS completely unusable by just leaving the window open until all the memory has been gobbled up. – onno Jul 05 '12 at 17:28
  • @onno I removed the `timeout` and ran it again. The memory doesn't come back immediately, but if you give it enough time it does slowly return to normal. I don't know enough about memory management to know what's happening under the hood, but my guess would be it just takes time to release all the memory addresses for all the cmd windows that were spawned, because, presumably it will be allocating new memory for each of the `cmd /c` commands and depending on how long you leave it, that could be a LOT! – Bali C Jul 06 '12 at 09:13
  • Another thing to check would be your RAM, how much memory has your computer got? – Bali C Jul 06 '12 at 09:14
  • I guess what it comes down to is the speed of your PC. Maybe the OS doesn't recover as there simply isn't enough memory left, even the fastest computers would eventually crash if you were continually taking up more RAM. – Bali C Jul 06 '12 at 09:18