Instead of building your hash structure in memory you might build it on disk instead. If you use a tied system like DB_File your code probably won't have to change very much. It will be slower but you won't have to worry about running out of memory, just disk space.