1

Say I want to run multiple, computationally complex commands on Windows command line.

Does it makes a difference, performance wise, if I combine them with &:

doSomething1 & doSomething2 & doSomething3

or run them one by one?

doSomething1
doSomething2
doSomething3

I'm concerned that if I chain too many commands with & I might run out of memory at some point.

9
  • You won't run out of memory because the next command is executed only when the prior is completed. Commented May 29 at 12:23
  • @ChanganAuto Yes, but is the memory used by prior command fully released once it's completed? Or is it keeping only the result of the prior command in memory?
    – overdriven
    Commented May 29 at 12:27
  • Yes, it's the exact same as running one by one. The OS' memory management is smarter than you think. Commented May 29 at 12:30
  • I believe when using &&, the command will only run if the previous one was succesful.
    – LPChip
    Commented May 29 at 12:31
  • Your not going to run out of memory
    – Ramhound
    Commented May 29 at 12:34

3 Answers 3

2

No, it doesn't make a difference.

I'm concerned that if I chain too many commands with & I might run out of memory at some point.

If you're working in Cmd.exe, most of your commands are external – that is, they aren't handled by Cmd.exe itself but rather involve starting an .exe as a separate process – which means their memory allocation only exists for as long as the .exe remains running. As soon as the process exits, all of its resources (allocated memory, open files, etc) are automatically cleaned up by the OS, without any involvement from Cmd.exe at all.1

In other words, resource cleanup is part of your OS process management, not part of the command shell, and the shape or style of the command line has no way of affecting it.

The concern would be valid for commands that are internal to the shell – Cmd.exe has very few, it's mostly PowerShell that lets you build large data structures right inside the shell's process – so in theory, if you had PowerShell functions or cmdlets written that deal with large amounts of data, their objects might linger around after the function returns; but eventually the CLR runtime will garbage-collect them long before it becomes an issue.


1 (The OS does keep a tiny bit of the process around until the invoker (Cmd.exe in this case) picks it up – the exit status code, specifically – but it's less than a kilobyte per process, and the very act of Cmd.exe waiting for the process to exit is what causes it to be collected, so a command shell is the exact kind of program where it's impossible for that to become an issue.)

1

No difference. The & symbol is used to combine multiple commands in one line in batch scripting.

@Echo off
Echo Hello
Echo World
pause

In one line:

@Echo off & Echo Hello & Echo World & pause
-1

There is no difference, but putting one command in each line looks better and is easier to debug if you have errors in your code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .