I'm concerned at the use of num_objects_dirty here in place of num_objects (as used below). It seems like you could have num_objects_dirty = 0 while num_objects = target_max_objects.