Git Product home page Git Product logo

upsilongc's Introduction

Custom GCs in .NET

Starting from .NET Core 2.0 coupling between Garbage Collector and the Execution Engine itself have been loosened. Prior to this version, the Garbage Collector code was pretty much tangled with the rest of the CoreCLR code. However, Local GC initiative in version 2.1 is already mature enough to start using it. The purpose of the exercise we are going to do is to prepare Zero Garbage Collector that replaces the default one. And then move to implement something more sophisticated.

So this repository currently contains two so-called Zero GCs and one real-world Upsilon GC:

Upsilon GC

The very first, real-world custom GC that will actually reclaim memory.

TODO: write initial description

Zero Garbage Collector

Zero Garbage Collector is the simplest possible implementation that in fact does almost nothing. It only allows you to allocate objects, because this is obviously required by the Execution Engine. Created objects are never automatically deleted and theoretically, no longer needed memory is never reclaimed. Why one would be interested in such a simple GC implementation? There are at least two reasons:

  • it is an excellent basis for the development of your own Garbage Collection mechanism. It provides the necessary functionality to make runtime work properly and you can build on top of that.
  • it may be interesting for special use cases like very short living applications or such that almost no allocate memory (you can come up with those concepts as No-alloc or Zero-alloc programming). In such case providing GC overhead is unnecessary and it may be wise to get rid of it. It is like making huge GC.TryStartNoGCRegion over all you application.

More motivation you may find in JVM's recently added Epsilon GC which is exactly the same concept, as its proposal explains:

"Develop a GC that handles memory allocation but does not implement any actual memory reclamation mechanism. Once the available Java heap is exhausted, the JVM will shut down."

Samples

It is super important to set the GC to workstation mode, instead of Server mode. As explained in articles below, even in custom GC the difference between Workstation and Server should be irreleveant, this setting influece how runtime handles write barriers. In case of Server GC it will end in StackOverflowException or any other nasty crash.

WebApi

There is simple WebAPI project doing almost nothing, just to provide you a quick-start scenario. You may wish load test it to see the behaviour of memory with ZeroGC (for example using SuperBenchmarker):

sb -u http://localhost:5000/api/values -c 60 -n 40000 -y 10 -W 60

Materials

You can find detailed description of the project:

upsilongc's People

Contributors

aensidhe avatar kkokosa 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

upsilongc's Issues

HRESULT 80004005

Hello,

When using the ZeroGC, my CLR can't start with an HRESULT 80004005 (E_FAIL):

Failed to create CoreCLR, HRESULT: 0x80004005

Do you have an idea why ? I have put the dll here: C:\Windows\Microsoft.NET\Framework64\v4.0.30319, also I had to add a function called GC_IsCompatibleWithRuntime() in the main, otherwise the ZeroGC wouldn't be selected (clr.dll was trying to GetProcAddress(GC_IsCompatibleWithRuntime)).

Understanding the GCDesc structure

The code to retrieve the references from the GCDesc has been rewritten in C# in ClrMD. It's slightly easier to understand than the C++ version: https://github.com/microsoft/clrmd/blob/master/src/Microsoft.Diagnostics.Runtime/src/Common/GCDesc.cs

From what I understand, in the GCDesc, for a given object, there are multiple series of references. It seems that each series is a group of contiguous references in the object. For instance, take an object that looks like:

[StructLayout(LayoutKind.Sequential)]
public class SomeObject
{
	[FieldOffset(0)]
	public object ref1;
	[FieldOffset(8)]
	public object ref2;
	[FieldOffset(24)]
	public object ref3;
}

(for all this message, I'm assuming 64 bits. Adjust everything accordingly if running in 32 bits).
In the layout of the objects, the references ref1 and ref2 are contiguous, then you have some unused space, then ref3. In the GCDesc you will have two series, one for the first two references and one for the last. When you think about it, series work a bit like a free-list.

How to read those series? The number of series is stored in the last long (once again I'm assuming 64 bits) of the GCDesc. All series are two IntPtr wide, and packed at the end of the GCDesc. In short, the GCDesc looks like:

[Some stuff][Series][Series][Series][Number of series]

So all those series are located between [End of GCDesc - sizeof(long) - numSeries * sizeof(long) * 2] and [End of GCDesc - sizeof(long)]

For each of those series, the first long is the size of the series. The second long is the offset of the first of the references in the object (so in the SomeObject example, the size of the first series would be 2 and the offset would be 0, the size of the second series would be 1 and the offset would be 24).

From there, you have everything you need to read the references. Assuming addr is the address of the object you want to browse, you enumerate the series from the corresponding GCDesc, and for each series you read [size of the series] addresses starting from addr + [offset of the series].

Note that there are two cases, depending on whether "number of series" is a positive number or negative. I've only dug into the positive case, but the negative case look very similar.

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.