This was mentioned on https://gerrit.wikimedia.org/r/#/c/208397/.
https://gerrit.wikimedia.org/r/#/c/208408/ fixes this but resulted in too much CPU time on the servers. Oddly enough it used batches of 500 instead of the 1000 from the prior code, which somehow worked before the OOM problem.