View Single Post
  #2  
Old June 25th, 2009, 07:13 AM
macbri's Avatar
macbri macbri is offline
Mac (r)evolution
 
Join Date: Jun 2005
Location: One of these days, Alice....
Posts: 299
Thanks: 3
Thanked 3 Times in 3 Posts
macbri has a spectacular aura aboutmacbri has a spectacular aura about
Two options: GNU awk, or an unsorted array

You've definitely found something -- running valgrind on awk with your example and again on GNU awk (from MacPorts) shows that sure enough awk leaks memory like a sieve, while GNU awk doesn't.

It might be worth filing a Bug Report with Apple (not that you'll ever hear back but it might at least call their attention to it and get it fixed in a future release).

Consider installing GNU awk, which is not only not afflicted by this apparent bug, but also more powerful. As a quick fix, if you don't want to install GNU awk right now, you can modify your code as shown below, and even the Apple-provided awk doesn't leak memory with this. There's one major caveat -- you're not guaranteed to have your array items in any particular order, so it may not be suitable for what you want to accomplish:

Code:
# Store the first 800 words 
NR < 801 { word[NR] = $1 }

# Now access a stored array value without a memory leak
NR > 800 {
    for (i in word)
        printf("%d\t%d\t%s\n", NR, i, word[i]);
}
__________________
Tech Blog

Last edited by macbri; June 25th, 2009 at 07:26 AM.
Reply With Quote