Less than a week after I posted about how I like my new dzen status bar, I discovered an annoying fact about Bash.
If you run the following snippet:
while :; do
sleep 0.001
done
You will see memory usage growing, but it’s not some kind of memory leak, because it eventually will stop at some point. I realized it’s about external command, which is sleep in this case. If you use builtin, e.g.
while :; do
read -t 0.001
done
The memory usage is fixed from the beginning. Somehow, calling external commands causes such situation. Unfortunately, it’s nearly impossible for a shell script not to use any external commands.
I was hoping, my status.sh would use some reasonable amount memory after growing, but it reached 100MB+ and still growing. I had no idea when it would stop eating memory, therefore I decided to port the script to C code. And there it is, status.c.
Now, the memory usage is 1,364KB (status.c) + 3,292KB (dzen2).
The C code doesn’t do more than the Bash script, in fact, I removed some small stuff. It’s basically almost same result but runs faster and efficiently.
It’s one-day work and my C skill isn’t good, the code is lack of error handling and testing, and many stuff are hard-coded. So, you know what to expect.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.