total available space for batch environment variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

total available space for batch environment variables

#1 Post by mirrormirror » 12 Feb 2016 17:11

Was looking this up and most places say 32k - here one:
https://blogs.msdn.microsoft.com/oldnew ... 0/?p=15083

Does this apply to modern x64 OS's (win7, 8, 10)?

I was reading a post here mentioning 5mb & 250mb so it sparked the question on the 32k limit:
viewtopic.php?f=3&t=6436&start=15
Ed Dyreen wrote:
ok, cool but now I have a different problem. I need to clean the DOS environment before I start a new instance of CMD. The batchfile I use to develop and test uses 250MB of memory when it reaches this point. Actual memory is only 5MB but in DOS, once memory is taken, it's never released even if the environment is cleared. It takes a long time to prepare the environment for a new instance:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: total available space for batch environment variables

#2 Post by foxidrive » 12 Feb 2016 21:52

mirrormirror wrote:Was looking this up and most places say 32k
Ed Dyreen wrote:


I think Ed is speaking about the entire environment memory area.

Edit: I may have misunderstood as the link you gave mentions the limit for a single environment variable, rather than total space.

The limit on many commands is 8 KB in modern Windows (some are less IIRC) and the command line itself has that 8KB limit - it was 2KB in an older Windows.

So manipulating a 32 KB variable might not be feasible for what you need to do anyway.

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: total available space for batch environment variables

#3 Post by mirrormirror » 12 Feb 2016 21:59

Edit: I may have misunderstood as the link you gave mentions the limit for a single environment variable, rather than total space.

The article title is in reference to a single variable but it mentions the entire environment space is 32k
All environment variables must live together in a single environment block, which itself has a limit of 32767 characters. But that count is the sum over all environment variable names and value...

Am I mis-understanding the article?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: total available space for batch environment variables

#4 Post by foxidrive » 12 Feb 2016 22:07

Ok. Is this useful for you?

The maximum size of a user-defined environment variable is 32,767 characters. There is no technical limitation on the size of the environment block. However, there are practical limits depending on the mechanism used to access the block. For example, a batch file cannot set a variable that is longer than the maximum command line length.


https://msdn.microsoft.com/en-us/librar ... 85%29.aspx

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: total available space for batch environment variables

#5 Post by mirrormirror » 12 Feb 2016 22:16

These two comments seem to contradict each other:
a single environment block, which itself has a limit of 32767 characters.

There is no technical limitation on the size of the environment block

Is he talking about two different "environment block"s ??

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: total available space for batch environment variables

#6 Post by foxidrive » 12 Feb 2016 22:21

mirrormirror wrote:These two comments seem to contradict each other:
a single environment block, which itself has a limit of 32767 characters.

There is no technical limitation on the size of the environment block

Is he talking about two different "environment block"s ??


Are you mixing pages? The MS link I posted doesn't have the word single on the page, according to Firefox search.

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: total available space for batch environment variables

#7 Post by mirrormirror » 12 Feb 2016 22:33

YES - I was mixing pages - sorry - my bad. It looks like the article I posted has inferior info. From the one you posted:
Windows Server 2003 and Windows XP: The maximum size of the environment block for the process is 32,767 characters. Starting with Windows Vista and Windows Server 2008, there is no technical limitation on the size of the environment block.

I guess the limits are gone after XP/2003
Thank you for your help :)

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: total available space for batch environment variables

#8 Post by Aacini » 12 Feb 2016 23:01

The total available space for Batch environment variables in all modern Windows versions (from XP-Pro on) is 64 MB (yes, 64 MegaBytes = 64 K * KBytes = 64*1024*1024 bytes).

on this Windows XP-Pro site, below "2nd Note", MicroSoft wrote:• The maximum individual environment variable size is 8192bytes.

• The maximum total environment variable size for all variables, which includes variable names and the equal sign, is 65,536KB.

For an example that deals with very large environment sizes, see this post.

Antonio

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: total available space for batch environment variables

#9 Post by mirrormirror » 13 Feb 2016 00:33

Thanks - this info is great.
Anyone know how to mark a topic as answered?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: total available space for batch environment variables

#10 Post by Ed Dyreen » 13 Feb 2016 00:35

on this Windows XP-Pro site, below "2nd Note", MicroSoft wrote:• The maximum total environment variable size for all variables, which includes variable names and the equal sign, is 65,536KB.
A tasklist /V dump of a batch program currently running on windows XP.

Code: Select all

cmd.exe  3604 Console 0 75.904 kB Actief ED-work-VM\Administrator 0:13:30 n.v.t.
This is well over 65,536KB. I have not been able to detect any limit. The largest session I ever ran consumed around 300MB. Why this claim is made is a complete mystery to me. I did detect failures to start some installers from such large environments, apparently some programs can't handle it while others can handle it just fine.
on this Windows XP-Pro site, below "2nd Note", MicroSoft wrote:• The maximum individual environment variable size is 8192bytes.
What they do not say is that you won't be able to set those last bytes.

Code: Select all

@echo off

::size=1
set "x=#"&set x
::size=2
set x=%x%%x%&set x
::size=4
set x=%x%%x%&set x
::size=8
set x=%x%%x%
set y=%x:~0,-6%size=8
set y
::size=16
set x=%x%%x%
set y=%x:~0,-7%size=16
set y
::size=32
set x=%x%%x%
set y=%x:~0,-7%size=32
set y
::size=64
set x=%x%%x%
set y=%x:~0,-7%size=64
set y
::size=128
set x=%x%%x%
set y=%x:~0,-8%size=128
set y
::size=256
set x=%x%%x%
set y=%x:~0,-8%size=256
set y
::size=512
set x=%x%%x%
set y=%x:~0,-8%size=512
set y
::size=1024
set x=%x%%x%
set y=%x:~0,-9%size=1024
set y
::size=2048
set x=%x%%x%
set y=%x:~0,-9%size=2048
set y
::size=4096
set x=%x%%x%
set y=%x:~0,-9%size=4096
set y
::size=8185
set x=%x%%x:~0,-7%
set y=%x:~0,-9%size=8185
set y
::size=8192
set y=%x:~0,-2%size=8192
set y
pause
exit
The variable name is part of this size and in this case has a length of one byte. If the complete command for size=8185 is expanded, it makes up a total of 8191 bytes. The set command is shorting the max assignment by at least 6 bytes.

Post Reply