If you role_assign somebody to the teacher role at site level on a large site (~5k courses, ~110k contexts), this runs out of memory.
There are three causes:
1) In forum_role_assign, it does get_records('course'). This should restrict to required fields (only id!) and should be a recordset anyway. Easily changed.
but more seriously
2) The get_fast_modinfo data is cached. This cache gradually expands to contain the complete modinfo for all courses.
3) get_fast_modinfo also causes contexts to be cached. This cache gradually expands to contain all contexts for courses, modules, and blocks (ie virtually the whole context table).
Petr suggested that the best approach would be to limit the size of these caches, discarding old records. I have implemented this. The changes appear to work (the memory issues go away) on our test system.
Could somebody review this patch please as it's for 1.9 and is not trivial?