powershell 中的内存使用情况

Memory usage in powershell

我正在尝试使用 Powershell 来处理围绕 运行 化学模型的很多很多迭代的平凡任务,输入文件略有不同。我的 powershell 脚本创建一个目录,将模型和输入文件复制到目录中,然后调用模型,该模型写入单个输出文件。模型完成后,脚本会在循环中再迭代一次并再次执行所有操作(我在下面包含了脚本)。我的问题是 powershell 的内存使用量不断上升,直到所有 RAM 都被占用。直接调用模型(即只需单击 .exe 文件)不会产生此类问题,它使用的内存非常少。 powershell 记住了哪些不必要的信息,我如何强制它保持内存清晰?我是 Powershell 的新手,所以非常简单的答案,基本上没有假设的先验知识,将不胜感激! 感谢您的帮助,

#say where the input and the output should be found:

$source='C:\a_directory_with_input_files'
$destination='C:\somewhere_for_model_output'

#unique name for this particular set of runs:
$suffix='sheath_data_v2_NH3_NO3_AIM'
$suffix2='AIM_sizes_NH3'

$names= '17_exp1' , '17_exp2',  '18_exp2',  '20am_exp1', '20am_exp2' , '20pm_exp1',  '20pm_exp2',  '20pm_exp3',  '21_exp1', '21_exp2',  '21_exp3', '05_exp1', '05_exp2', '07_exp1', '07_exp2', '07_exp3'

#find the bits of name for the directories:
ForEach ($i in $names)
{

echo $i
if ($i -eq '17_exp1') {$fn='17_11_2013_15_00-15_41_expa1_NH4'}
if ($i -eq '17_exp2') {$fn='17_11_2013_16_59-17_49_expa2_NH4'}
if ($i -eq '18_exp2') {$fn='18_11_2013_04_22-05_30_expa2_NH4'}
if ($i -eq '20am_exp1') {$fn='20_11_2013_02_26-03_19_expa1_NH4'}
if ($i -eq '20am_exp2') {$fn='20_11_2013_08_32-09_15_expa2_NH4'}
if ($i -eq '20pm_exp1') {$fn='20_11_2013_1508-1551_expa1_NH4'}
if ($i -eq '20pm_exp2') {$fn='20_11_2013_2015-2110_expa2_NH4'}
if ($i -eq '20pm_exp3') {$fn='20_11_2013_2240-2335_expa3_NH4'}
if ($i -eq '21_exp1')   {$fn='21_11_2013_03_51-04_58_expa1_NH4'}
if ($i -eq '21_exp2') {$fn='21_11_2013_06_52-07_44_expa2_NH4'}
if ($i -eq '21_exp3') {$fn='21_11_2013_08_57-09_53_expa3_NH4'}
if ($i -eq '05_exp1') {$fn='05_12_2013_11_59-12_40_expa1_NH4'}
if ($i -eq '05_exp2') {$fn='05_12_2013_13_53-14_30_expa2_NH4'}
if ($i -eq '07_exp1') {$fn='07_12_2013_10_04-10_45_expa1_NH4'}
if ($i -eq '07_exp2') {$fn='07_12_2013_11_26-12_06_expa2_NH4'}
if ($i -eq '07_exp3') {$fn='07_12_2013_12_58-13_40_expa3_NH4'}
if ($i -eq '25_pt1_C9_exp1') {$fn='25_09_2014_19_40-20_15_expa1'}
if ($i -eq '25_pt1_C9_exp2') {$fn='25_09_2014_21_00-21_31_expa2'}
if ($i -eq '25_pt1_C9_exp3') {$fn='25_09_2014_22_11-22_41_expa3'}
if ($i -eq '28_pt1_C9_exp1') {$fn='28_09_2014_16_40-17_06_expa1'}
if ($i -eq '28_pt1_C9_exp2') {$fn='28_09_2014_17_44-18_10_expa2'}
if ($i -eq '28_pt1_C9_exp3') {$fn='28_09_2014_18_52-19_15_expa3'}
if ($i -eq '28_pt1_C9_exp4') {$fn='28_09_2014_19_54-20_20_expa4'}
if ($i -eq '24_pt2_C9_exp1') {$fn='24_09_2014_16_22-16_50_expa1'}
if ($i -eq '24_pt2_C9_exp2') {$fn='24_09_2014_17_35-18_00_expa2'}
if ($i -eq '24_pt2_C9_exp3') {$fn='24_09_2014_18_47-19_15_expa3'}
if ($i -eq '25_pt2_C9_exp1') {$fn='25_09_2014_23_45-00_10_expa1'}
if ($i -eq '25_pt2_C9_exp2') {$fn='26_09_2014_01_02-01_30_expa2'}
if ($i -eq '25_pt2_C9_exp3') {$fn='26_09_2014_02_14-02_40_expa3'}
if ($i -eq '28_pt2_C9_exp1') {$fn='28_09_2014_20_55-21_20_expa1'}
if ($i -eq '28_pt2_C9_exp2') {$fn='28_09_2014_22_12-22_41_expa2'}
if ($i -eq '28_pt2_C9_exp3') {$fn='28_09_2014_23_31-23_55_expa3'}


#create the new directory, and copy input and model files into it:
$new_dir=$i+$suffix
echo $new_dir

new-item $destination$new_dir -itemtype directory

copy-item $source\work\PSI\CLOUD\Windows_modelling\models\chamber_model_$suffix2".exe" $destination$new_dir\

copy-item $source\work\PSI\CLOUD\Windows_modelling\expansion_input_files\input_chamber_$fn.dat $destination$new_dir\input_chamber.dat
copy-item $source\work\PSI\CLOUD\Windows_modelling\libraries_and_stuff\* $destination$new_dir\
copy-item $source\work\PSI\CLOUD\Windows_modelling\AIM_input\AMS_frac_$fn$suffix.dat $destination$new_dir\AMS_frac.dat
copy-item $source\work\PSI\CLOUD\Windows_modelling\AIM_input\dist_$fn$suffix.dat $destination$new_dir\dist.dat
copy-item $source\work\PSI\CLOUD\Windows_modelling\AIM_input\traj_$fn$suffix.dat $destination$new_dir\traj.dat
copy-item $source\work\PSI\CLOUD\Windows_modelling\AIM_input\*$fn$suffix.dat $destination$new_dir\

#now go there:
cd $destination$new_dir
# and call the model, dumping any of the stuff the model outputs to the screen:
$(
./chamber_model_AIM_sizes_NH3.exe
) | Out-Null

}

当您使用命令 $(blah.exe) | out-null 时,这会导致 Powershell 将 blah.exe 的所有输出保留在内存中。我在我的系统上测试了一个单行脚本:

cmd /c "dir c:\*.* /s" | out-null

Powershell 内存使用率没有攀升。但是:

$(cmd /c "dir c:\*.* /s") | out-null

Powershell 内存立即开始攀升并持续攀升。