Skip to content

runtime: ReadMemStats called in a loop may prevent GC #40459

Description

@mknyszek

In rolling forward the SetMaxHeap patches, I discovered that, as a result of the fix to #19112, if ReadMemStats is called in a tight loop, then it might keep stomping on other goroutines trying to stop the world by reacquiring worldsema before others get the chance. The worst case is if it stomps on a goroutine trying to stop the world for either starting or stopping a GC, which messes with pacing significantly.

The good news is, ReadMemStats is known to be a heavyweight operation, and so isn't called often. This is really only a problem if it's called in a tight loop. In the reproduction "tight loop" consisted of a single goroutine performing a large allocation, calling ReadMemStats, then doing some arithmetic and predictable branches. Adding in even a 1 ms sleep is enough to resolve the issue. This case is more likely to appear in tests and examples, where the behavior will be unexpected.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions