Giter Site home page Giter Site logo

gcnotifier's Introduction

gcnotifier

gcnotifier provides a way to receive notifications after every run of the garbage collector (GC). Knowing when GC runs is useful to instruct your code to free additional memory resources that it may be using.

GoDoc GoCover

Why?

Package gcnotifier provides a way to receive notifications after every garbage collection (GC) cycle. This can be useful, in long-running programs, to instruct your code to free additional memory resources that you may be using.

A common use case for this is when you have custom data structures (e.g. buffers, rings, trees, pools, ...): instead of setting a maximum size to your data structure you can leave it unbounded and then drop all (or some) of the allocated-but-unused slots after every GC run (e.g. sync.Pool drops all allocated-but-unused objects in the pool during GC).

To minimize the load on the GC the code that runs after receiving the notification should try to avoid allocations as much as possible, or at the very least make sure that the amount of new memory allocated is significantly smaller than the amount of memory that has been "freed" by your code.

GCNotifier guarantees to send a notification after every GC cycle completes. Note that the Go runtime does not guarantee that the GC will run: specifically there is no guarantee that a GC will run before the program terminates.

How to use it

The simplest use of this library is as follows:

gcn := gcnotifier.New()
for range gcn.AfterGC() {
  // this code will be executed after every GC cycle
}
runtime.KeepAlive(gcn) // or store gcn somewhere to keep it alive

As written, the loop above will never terminate, so it is mostly useful if you have global caches that persist for the whole duration of the process.

Note that the AfterGC() call is not guarantee to keep gcn alive. gcn and any channel returned by AfterGC() will automatically get closed if gcn is garbage collected. So if gcn is not kept alive, the for loop will terminate unexpectedly.

If you want to ensure the loop above terminates (e.g. because you only need the notifications for the lifetime of a different object) you can call the Close() method:

gcn := gcnotifier.New()
go func() {
  for range gcn.AfterGC() {
    // this code will be executed after every GC cycle
    // until Close() is called
  }
}()

// later, or elsewhere in your code
gcn.Close() // the loop above will terminate some time after this call

Note that if a loop iteration takes longer than the interval between two GC cycles it is possible that one notification will be dropped. Followup notifications will be still received correctly.

For a more complex example of how to use it have a look at Example() in gcnotifier_test.go.

For details have a look at the documentation.

How it works

gcnotifier uses finalizers to know when a GC run has completed.

Finalizers are run when the garbage collector finds an unreachable block with an associated finalizer.

The SetFinalizer documentation notes that there is no guarantee that finalizers will run before a program exits. This doesn't mean, as sometimes incorrectly understood, that finalizers are not guaranteed to run at all, it just means that they are not guaranteed to run because GC itself is not guaranteed to run in certain situations: e.g. when the runtime is shutting down. Finalizers can also not run for other reasons (e.g. zero-sized or package-level objects) but they don't apply to gcnotifier because care was taken in the implementation to avoid them.

The only other case in which a notification will not be sent by gcnotifier is if your code hasn't consumed a previously-sent notification. In all other cases if a GC cycle completes your code will receive a notification.

The test in gcnotifier_test.go generates garbage in a loop and makes sure that we receive exactly one notification for each of the first 500 GC runs. In my testing I haven't found a way yet to make gcnotifier fail to notify of a GC run short of shutting down the process or failing to receive the notification. If you manage to make it fail in any other way please file a GitHub issue.

License

MIT

Author

Carlo Alberto Ferraris (@cafxx)

gcnotifier's People

Contributors

cafxx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gcnotifier's Issues

Doesn't always work

import "runtime/debug"
go func() {
		debug.SetGCPercent(-1)
		go func() {
			ticker := time.NewTicker(30 * time.Second)
			for {
				select {
				case <-ticker.C:
					runtime.GC()
				}
			}
		}()

		gcn := gcnotifier.New()
		for range gcn.AfterGC() {
			fmt.Println("Garbage Collected") // Not always detected
		}
	}()

It doesn't always work even when I have a finalizer for an object that informs me that GC has actually run.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.