In here, use the serializeStream method of spark.Serializer instead of creating a giant ByteBuffer. It will probably be more efficient for Seq types by serializing each object one by one.