False sharing occurs when two programs access the same cache line, causing one program to reload its cache with updated information that the other program has modified. This wastes processing power and resources. Prevention methods include adding empty information or limiting the frequency of reading and writing on the cache line.
False sharing is a situation that can occur in a computer program when two applications running simultaneously try to access information in the same area of logical memory that each program or process has stored in its cache. The data in each application’s cache is copied from a common source, so changing one cache causes the other to have to be reloaded from the source. The false sharing aspect arises when changes made to the cache line by one program don’t actually affect the data the second program is using, in which case forcing the cache to be reloaded is a waste of system resources and can negatively impact performance of the program. The false sharing problem is difficult to detect, because it is not a direct result of any code contained within applications, although there are effective ways to prevent false sharing, such as moving data that is in a currently stored row cached on a separate line.
One of the main reasons false sharing can occur is in how an operating system or hardware handles reading and writing data. When information from a hard drive or other source is read or written by a program, it is usually loaded into a temporary cache so that it can be accessed quickly. The amount of information cached by the source location is called a cache line and is a logical block of memory that can typically be between 2 bytes and 256 bytes in length. In some operating systems or system architectures, programs can run concurrently, for example in parallel processing. This means that it is possible for two separate programs to try to access information in the same line of the cache, so each program will have its own copy of the source data, potentially causing changes in one cache to not be reflected in the other cache, invalidating the data it contains.
There are a number of mechanisms employed to handle the situation when two programs attempt to access the same cache line, but the result most often is that one program is forced to reload its cache with updated information that the other program has modified. This type of crash is known as false sharing when the data within the cache line that each program is accessing is unrelated, so forcing a program to reload the cache is a waste of processing power and other resources. This is most often a problem for a program that will never modify the information in its cache, so there is no risk of the application overwriting the changes made by the other process.
There are two prevalent ways to prevent or mitigate the effects of false sharing. The first is to add empty information before or after the data in memory, essentially forcing it into a separate cache line not accessed by another program. The second method is to limit the frequency of reading and writing on the cache line so that it is reloaded as little as possible. More complex solutions involve management at the cache level or even changes to how an operating system handles sharing.
Protect your devices with Threat Protection by NordVPN