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

  • SEARCH
  • Home
  • 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 8385033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:33:57+00:00 2026-06-09T17:33:57+00:00

Context: this is based on a question that was asked and then deleted before

  • 0

Context: this is based on a question that was asked and then deleted before I could answer it – but I think it is a good question, so I’ve tidied it, rephrased it, and re-posted it.

In a high-throughput scenario using protobuf-net, where lots of allocations are a problem (in particular for GC), is it possible to re-use objects? For example by adding a Clear() method?

[ProtoContract]
public class MyDTO
{
    [ProtoMember(1)]
    public int Foo { get; set; }
    [ProtoMember(2)]
    public string Bar { get; set; }
    [ProtoMember(3, DataFormat = DataFormat.Group)]
    public List<int> Values { get { return values; } }
    private readonly List<int> values = new List<int>();

    public void Clear()
    {
        values.Clear();
        Foo = 0;
        Bar = null;
    }
}
  • 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-06-09T17:33:58+00:00Added an answer on June 9, 2026 at 5:33 pm

    protobuf-net will never call your Clear() method itself, but for simple cases you can simply do this yourself, and use the Merge method (on the v1 API, or just pass the object into Deserialize in the v2 API). For example:

    MyDTO obj = new MyDTO();
    for(...) {
        obj.Clear();
        Serializer.Merge(obj, source);        
    }
    

    This loads the data into the existing obj rather than creating a new object each time.

    In more complex scenarios where you want to reduce the number of object allocations, and are happy to handle the object pooling / re-use yourself, then you can use a custom factory. For example, you can add a method to MyDTO such as:

    // this can also accept serialization-context parameters if
    // you want to pass your pool in, etc
    public static MyDTO Create()
    {
        // try to get from the pool; only allocate new obj if necessary
        return SomePool.GetMyDTO() ?? new MyDTO();
    }
    

    and, at app-startup, configure protobuf-net to know about it:

    RuntimeTypeModel.Default[typeof(MyDTO)].SetFactory("Create");
    

    (SetFactory can also accept a MethodInfo – useful if the factory method is not declared inside the type in question)

    With this, what should happen is the factory method is used instead of the usual construction mechanisms. It remains, however, entirely your job to cleanse (Clear()) the objects when you are finished with them, and to return them to your pool. What is particularly nice about the factory approach is that it will work for new sub-items in lists, etc, which you can’t do just from Merge.

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

Sidebar

Related Questions

Firstly I appreciate that this question could be seen as subjective but I strongly
I can see that this question has been asked several times, but none of
OK, I had asked a similar question before and had got an answer but
I recently asked this question regarding pulling beans out of an applicationContext. The answer
UPDATE (2010-12-21): Completely rewrote this question based on tests that I've been doing. Also,
First off, I apologize if this question has been asked before. I've done a
I know, this question was asked many times and answered as well.. But, my
There is a similarly worded question, but I think this is slightly different. Basically,
Being spotted in context of this question this seemingly inconsistent behavior can be reproduced
First of all, the context of this question is: Cannot install psycopg2 on OSX

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.