"少"为gzip文件上的管道输出消耗大量RAM,为什么?

时间:2015-04-04 18:38:34

标签: linux bash shell gzip less-unix

请原谅我前期无用的catecho,但在~2GB .gz文件上运行less时,我看到大约有25GB的内存耗尽(尽管输出被输入awk并在那里消耗):

[user@mybox:~]$ cat <(echo '173abcde7665559.90651926
131abcde7298936.49040546
... (25 lines total here) ...
186abcde4858463.43044639
163abcde9409643.80726489'|awk '{print "KEY 1"length($1)-16":"$1}';
less /tmp/stats.gz)|awk '{if("KEY"==$1){K[$2]=1}else{if($8 in K)print}}' >bad25&

我期望上面完成而不需要RAM,但令我惊讶的是它在2.5小时之后的样子(当读取.gz时为89.8%):

[user@mybox:~]$ ps auxf|grep -e 'pts/2' -e PID |grep -v grep
USER   PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user 26896  0.0  0.0  68356  1580 pts/2    Ss+  15:23   0:00  \_ /bin/bash
user 27927  0.7  0.0  58932   476 pts/2    S    15:23   1:00      \_ cat     /dev/fd/63
user 27929  0.0  0.0  68356   716 pts/2    S+   15:23   0:00      |   \_ /bin/bash
user 27932 99.9 75.0 22389852 18512388 pts/2 R+ 15:23 137:42      |       \_ less /tmp/stats.gz
user 27933  0.0  0.0  65928  1168 pts/2    S+   15:23   0:00      |           \_ /bin/sh - /usr/bin/lesspipe.sh /tmp/stats.gz
user 27934  1.3  0.0   4176   492 pts/2    S+   15:23   1:52      |               \_ gzip -dc -- /tmp/stats.gz
user 27928  2.1  0.0  63908   776 pts/2    S    15:23   2:56      \_ awk {if("KEY"==$1){K[$2]=1}else{if($8 in K)print}}
[user@mybox:~]$ free -m
             total       used       free     shared    buffers     cached
Mem:         24103      23985        117          0        125       3958
-/+ buffers/cache:      19901       4201
Swap:         8191       7914        277
[user@mybox:~]$ echo 1|awk "{print `cat /proc/27934/fdinfo/3|sed -n 's/pos:[ \t]*//p'`/`du -b /tmp/stats.gz|sed 's/[ \t].*//'`}";date
0.898284
Sat Apr  4 17:41:24 GMT 2015

我会尝试其他一些选项(比如用直接gzip -dczcat重写我的命令)以查看这些选项是否有帮助,但是如果有人可以知道为什么会发生这种情况更少(或任何其他命令),是否在更高版本中修复了已知的lessbash错误?也许一些shell技巧可以减少表现不正常的行为?

P.S。 stats.gz是未压缩的25261745373字节(包围MAX_INT的5次):

[user@mybox:~]$ ls -l /tmp/stats.gz
-rw-r--r-- 1 user users 1837966346 Apr  3 21:42 /tmp/stats.gz
[user@mybox:~]$ gzip -l /tmp/stats.gz
         compressed        uncompressed  ratio uncompressed_name
         1837966346          3786908893  51.5% /tmp/stats

1 个答案:

答案 0 :(得分:4)

less将所有数据存储在内存中。这是允许您向上滚动的内容。