The problem is on these two lines:
source << tmp_src
source << tmp_src.gsub( /\s{2,}/, "\n" )
When you read a large file you are slowly growing a very large string in memory.
The simplest solution is not to use this temporary source
string at all, but to write the results directly to the file. Just replace those two lines with this instead:
# source << tmp_src
out1.write(tmp_src)
# source << tmp_src.gsub( /\s{2,}/, "\n" )
out1.write(tmp_src.gsub( /\s{2,}/, "\n" ))
This way you're not creating any big temporary strings in memory and it should work better (and faster) this way.