Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:43:58+00:00 2026-05-27T22:43:58+00:00

Suppose we have to create many small objects of byte array type. The size

  • 0

Suppose we have to create many small objects of byte array type. The size varies but it always below 1024 bytes , say 780,256,953….

Will it improve operator new or GC efficiency over time if we always allocate only bytes[1024], and use only space needed?

UPD: This is short living objects, created for parsing binary protocol messages.

UPD: The number of the objects is the same in both cases, it just the size of allocation which changes (random vs. always 1024).

In C++ it would matter because of fragmentation and C++ new performance. But in C#….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:43:58+00:00Added an answer on May 27, 2026 at 10:43 pm

    Will it improve operator new or GC efficiency over time if we always allocate only bytes[1024], and use only space needed?

    Maybe. You’re going to have to profile it and see.

    The way we allocate syntax tree nodes inside the Roslyn compiler is quite interesting, and I’m eventually going to do a blog post about it. Until then, the relevant bit to your question is this interesting bit of trivia. Our allocation pattern typically involves allocating an “underlying” immutable node (which we call the “green” node) and a “facade” mutable node that wraps it (which we call the “red” node). As you might imagine, it is frequently the case that we end up allocating these in pairs: green, red, green, red, green, red.

    The green nodes are persistent and therefore long-lived; the facades are short-lived, because they are discarded on every edit. Therefore it is frequently the case that the garbage collector has green / hole / green / hole / green / hole, and then the green nodes move up a generation.

    Our assumption had always been that making data structures smaller will always improve GC performance. Smaller structures equals less memory allocated, equals less collection pressure, equals fewer collections, equals more performance, right? But we discovered through profiling that making the red nodes smaller in this scenario actually decreases GC performance. Something about the particular size of the holes affects the GC in some odd way; not being an expert on the internals of the garbage collector, it is opaque to me why that should be.

    So is it possible that changing the size of your allocations can affect the GC in some unforseen way? Yes, it is possible. But, first off, it is unlikely, and second it is impossible to know whether you are in that situation until you actually try it in real-world scenarios and carefully measure GC performance.

    And of course, you might not be gated on GC performance. Roslyn does so many small allocations that it is crucial that we tune our GC-impacting behaviour, but we do an insane number of small allocations. The vast majority of .NET programs do not stress the GC the way we do. If you are in the minority of programs that stress the GC in interesting ways then there is no way around it; you’re going to have to profile and gather empirical data, just like we do on the Roslyn team.

    If you are not in that minority, then don’t worry about GC performance; you probably have a bigger problem somewhere else that you should be dealing with first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an array of nodes (objects). I need to create a duplicate
Suppose I create collection like Collection<IMyType> coll; Then I have many implelentations of IMyTypem
Suppose I have a user defined type: CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT
Suppose I have 'film' objects like the one below in my collection. Films have
Suppose I have a table like: create table { id numeric(5,3), code varchar(10) }
Suppose I have a stored procedure that manages its own transaction CREATE PROCEDURE theProc
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose we have a class. We create an object from the class and when
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Suppose I have a table of customers: CREATE TABLE customers ( customer_number INTEGER, customer_name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.