You're copying over 2 million strings in the subroutine. Perl needs to allocate a lot of memory for that (in small chunks). Memory allocation doesn't scale very well (specially not when the process needs to swap). Do you need all the lines to be copied? Do you actually need to store each line of every file in memory first, before you do any processing?