WIPIVERSE All articles

Thrashing (computer science)

Thrashing is a performance degradation condition that occurs in computer systems using memory paging when the system's real memory (RAM) resources are overcommitted, leading to a constant state of paging (swapping pages to and from disk) and page faults. This condition causes application-level processing to slow dramatically, and system performance may degrade or even collapse. The situation can persist indefinitely until the user closes running applications or active processes free up additional virtual memory resources.

Mechanism

In virtual memory systems, physical main memory operates as a cache for virtual memory, which is generally stored on disk in memory pages. Operating systems allocate processes a virtual address space, and virtual addresses must be translated to physical addresses during execution. Active memory pages exist in both RAM and on disk; inactive pages are removed from memory and written to disk when main memory becomes full.

Most programs, after initialization, operate on a small number of code and data pages relative to their total memory requirements. The pages most frequently accessed at any given time are collectively called the working set, which can change over time. Virtual memory systems work efficiently when the working set is not significantly greater than the total number of available real storage page frames. However, as the combined working sets of running processes grow, page-fault resolution remains manageable only up to a critical threshold. Beyond that point, the number of page faults increases dramatically, and the time spent resolving them overwhelms the time spent performing useful computation. This condition is known as thrashing.

Causes

Thrashing in paged virtual memory systems may be caused by programs or workloads that exhibit insufficient locality of reference. If a program's or workload's working set cannot be effectively held within physical memory, constant data swapping occurs. Thrashing may also occur when a program randomly accesses large data structures, because its large working set produces continual page faults. Satisfying these page faults may require freeing pages that will soon have to be re-read from disk, perpetuating the cycle.

The term "thrashing" was first used during the era of tape operating systems to describe the sound tapes made when data was being rapidly written to and read.

Consequences

During thrashing, the CPU spends more time performing swapping and page-management work than executing productive application code. System throughput and latency may degrade by multiple orders of magnitude, depending on configuration and the algorithms involved. In modern computers, thrashing may occur in the paging system, in the I/O communications subsystem (particularly in conflicts over internal bus access), or in other parts of the memory hierarchy.

Mitigation and Prevention

A system experiencing thrashing is often the result of a sudden spike in page demand from a small number of running programs. Several approaches are used to address the problem:

  • Swap-token mechanism: A lightweight, dynamic thrashing protection mechanism in which a token is randomly assigned to a process experiencing page faults. The token holder is granted the privilege to allocate additional physical memory pages to build its working set, enabling it to finish execution and release memory to other processes. A timestamp is used to hand off the token sequentially. The first version was implemented in Linux; a second version, called preempt swap-token, assigns the token to the process with the highest number of swap-out pages and uses priority-dependent time stamps.

  • Working-set model: Managing the number of pages allocated to processes to ensure that the total working sets of active processes fit within available physical memory.

Related Phenomena

The term "thrashing" is also used to describe analogous phenomena in other computing contexts:

  • Cache thrashing: Occurs when main memory is accessed in a pattern that causes multiple memory locations to compete for the same cache lines, resulting in excessive cache misses. This is most problematic for caches with limited associativity. A classic example is a strided increment loop that accesses memory locations mapping to the same cache set.

  • TLB thrashing: Occurs when the translation lookaside buffer (TLB), which caches virtual-to-physical address translations, is too small for the working set of pages. This can occur even when instruction and data caches are not thrashing, because address lookup is performed at the page level rather than the cache-line level.

  • Heap thrashing: Refers to frequent garbage collection caused by insufficient free memory or memory fragmentation, resulting in failure to allocate memory for objects.

  • Process thrashing: Occurs when a process working set cannot be coscheduled—that is, when interacting processes are not all scheduled to run at the same time—causing them to be repeatedly scheduled and unscheduled while progressing slowly.

Historical and Scholarly Context

The concept of thrashing was formally analyzed by Peter J. Denning in his 1968 paper "Thrashing: Its causes and prevention," presented at the AFIPS Fall Joint Computer Conference. The term has since become a standard concept in operating systems and computer architecture literature.

See Also

  • Page replacement algorithm
  • Congestion collapse
  • Resource contention
  • Out of memory
  • Virtual memory
Browse

More topics to explore

    Browse all articles