This simple batch file in relatively short order consumes all available memory on Windows 7 (x64). What's going on? and what precautions can be taken to ward against it?
any-invalid-command-you-like-here ^
Apparently necessary preconditions to exhibit the effect:
^
is the very last thing in the file, and the script is not terminated with a newline.^
, while this one will ..^
(just slowly)In this Process Explorer screen shot, the script had been running about 30 seconds, consumed 2.9GB, and was still climbing at a steady rate:
If you're going to experiment with this, make sure you can get at the Close Window [X] control or have a Task Manager or Process Explorer fired up and ready as Ctrl-C, Ctrl-Break, Alt-F4 have no effect.
It appears multiple carets will cause the memory usage to ramp up much more quickly. The first time I encountered this there wasn't enough memory available in 1 or 2 minutes to do simple things like Alt-Tab and even the 3 finger salute Ctrl-Alt-Del was ineffective. I had to hard power off the machine.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.
What it is: %0|%0 is a fork bomb. It will spawn another process using a pipe | which runs a copy of the same program asynchronously. This hogs the CPU and memory, slowing down the system to a near-halt (or even crash the system).
When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.
The cause of this (from my understanding) is due to the cmd interpreter looking for a character to escape since the ^
is the batch escape character. Instead of properly identifying the end of file eof
in this scenario, cmd just keeps looping and initializing something while looking for the character to escape.
Reproduced on Windows 8 Pro (64) with cc^^^
(Multiple carets used to speed up the leak).
cc^
infinite loop and leaks very slowly.
cc^^
crashes with normal invalid command error.
cc^^^
infinite loop and leaks faster.
cc ^
infinite loop and leaks very slowly.
cc ^^
crashes with normal invalid command error.
cc ^^^
infinite loop and leaks faster.
cc"^
crashes with normal invalid command error.
cc"^^
crashes with normal invalid command error.
cc"^^^
crashes with normal invalid command error.
^
are used literally (outside of quotations). When quotation added the script crashes with standard invalid command error.^
(0x5E) or at least an odd number of carets.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With