Consider using the following code:
INSTANCES_CHUNK_SIZE = 5000
number_of_instances = 87653
count = number_of_instances
while (count > INSTANCES_CHUNK_SIZE) {
  return unless read_instances_chunk(filename, file, INSTANCES_CHUNK_SIZE)
  GC.start
  count -= INSTANCES_CHUNK_SIZE
}
return unless read_instances_chunk(filename, file, count)
GC.start
file.close