How are you determining that the memory is consumed by the bulk write?
Based on my own experience I'd have expressed doubts that it's an mgo driver leak.
I've got the mgo.v2 drivers in use for a project and it's been running a daemon process for about 2 months now. It periodically spins up and writes bulk records into mongo. It's capped at 1k updates per call to Run, but runs off an input queue, so it might run a number of 1k updates in a row. When I dump the memory profile it shows effectively flat memory usage.
The relevant section of my code is:
// send active values to mongodb for update
bulk := session.DB(dao.database).C(dao.collection).Bulk()
bulk.Unordered()
for _, v := range apply {
field := fmt.Sprintf("%s.%s", v.Register, v.Monitor)
bulk.Update(bson.M{"_id": v.Path}, bson.M{"$push": bson.M{field: v}})
}
_, err := bulk.Run()
// send any errors that we received and close/nil the channels for those
// resources
if err != nil {
bulkErr, ok := err.(*mgo.BulkError)
if ok {
for _, caseErr := range bulkErr.Cases() {
I've attached a pprof snapshot to show what the memory profile looks like after it's been running for ~60 days.