Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new .NET Core 3.0 API to get the total number of allocated bytes for all threads #1155

Merged
merged 3 commits into from
May 17, 2019

Conversation

adamsitnik
Copy link
Member

preview6 of .NET Core 3.0 exposes a new method that allows to get the number of allocated bytes for all threads. By using it we can fix #1153 and #723

Sample:

public class MultiThreadedAllocation
{
    public const int Size = 1_000_000;
    public const int ThreadsCount = 10;

    private Thread[] threads;

    [IterationSetup]
    public void SetupIteration()
    {
        threads = Enumerable.Range(0, ThreadsCount)
            .Select(_ => new Thread(() => GC.KeepAlive(new byte[Size])))
            .ToArray();
    }

    [Benchmark]
    public void Allocate()
    {
        foreach (var thread in threads)
        {
            thread.Start();
            thread.Join();
        }
    }
}
dotnet run -c Release -f netcoreapp2.1 --filter *MultiThreadedAllocation* --runtimes netcoreapp3.0 netcoreapp2.1 net461 --memory 
Method Runtime Toolchain Gen 0 Gen 1 Gen 2 Allocated
Allocate Clr net461 2000.0000 2000.0000 2000.0000 10024816 B
Allocate Core netcoreapp2.1 2000.0000 2000.0000 2000.0000 -
Allocate Core netcoreapp3.0 2000.0000 2000.0000 2000.0000 10001360 B

@adamsitnik
Copy link
Member Author

/cc @stephentoub

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@AndreyAkinshin
Copy link
Member

@adamsitnik 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use GC.GetTotalAllocatedBytes when available in MemoryDiagnoser
3 participants