@@ -259,23 +259,34 @@ export class Buffer implements IBuffer {
259259 this . _memoryCleanupQueue . clear ( ) ;
260260 // schedule memory cleanup only, if more than 10% of the lines are affected
261261 if ( dirtyMemoryLines > 0.1 * this . lines . length ) {
262+ this . _memoryCleanupPosition = 0 ;
262263 this . _memoryCleanupQueue . enqueue ( ( ) => this . _batchedMemoryCleanup ( ) ) ;
263264 }
264265 }
265266
266267 private _memoryCleanupQueue = new IdleTaskQueue ( ) ;
268+ private _memoryCleanupPosition = 0 ;
267269
268270 private _batchedMemoryCleanup ( ) : boolean {
271+ let normalRun = true ;
272+ if ( this . _memoryCleanupPosition >= this . lines . length ) {
273+ // cleanup made it once through all lines, thus rescan in loop below to also catch shifted lines,
274+ // which should finish rather quick if there are no more cleanups pending
275+ this . _memoryCleanupPosition = 0 ;
276+ normalRun = false ;
277+ }
269278 let counted = 0 ;
270- for ( let i = 0 ; i < this . lines . length ; i ++ ) {
271- counted += this . lines . get ( i ) ! . cleanupMemory ( ) ;
272- // throttle to 500 lines at once and
273- // return true to indicate, that the task is not finished yet
274- if ( counted > 500 ) {
279+ while ( this . _memoryCleanupPosition < this . lines . length ) {
280+ counted += this . lines . get ( this . _memoryCleanupPosition ++ ) ! . cleanupMemory ( ) ;
281+ // cleanup max 100 lines per batch
282+ if ( counted > 100 ) {
275283 return true ;
276284 }
277285 }
278- return false ;
286+ // normal runs always need another rescan afterwards
287+ // if we made it here with normalRun=false, we are in a final run
288+ // and can end the cleanup task for sure
289+ return normalRun ;
279290 }
280291
281292 private get _isReflowEnabled ( ) : boolean {
0 commit comments