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 8465643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:06:34+00:00 2026-06-10T15:06:34+00:00

I am definitely missing something very obvious but can anyone explain why there is

  • 0

I am definitely missing something very obvious but can anyone explain why there is a lot better compression rate in second case?!

Case 1: very low compression and sometimes even growth in size.

using (var memoryStream = new System.IO.MemoryStream())
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
  new BinaryFormatter().Serialize(gZipStream, obj);
  gZipStream.Close();
  return memoryStream.ToArray();
}

Case 2: a lot better compression and I did not get a size growth.

using (MemoryStream msCompressed = new MemoryStream())
using (GZipStream gZipStream = new GZipStream(msCompressed, CompressionMode.Compress))
using (MemoryStream msDecompressed = new MemoryStream())
{
  new BinaryFormatter().Serialize(msDecompressed, obj);
  byte[] byteArray = msDecompressed.ToArray();

  gZipStream.Write(byteArray, 0, byteArray.Length);
  gZipStream.Close();
  return msCompressed.ToArray();
}

I have done mirrored decompression and in both cases I can deserialize it into source object without any issues.

Here are some stats:

UncSize: 58062085B, Comp1: 46828139B, 0.81%

UncSize: 58062085B, Comp2: 31326029B, 0.54%

UncSize: 7624735B, Comp1: 7743947B, 1.02%

UncSize: 7624735B, Comp2: 5337522B, 0.70%

UncSize: 1237628B, Comp1: 1265406B, 1.02%

UncSize: 1237628B, Comp2: 921695B, 0.74%

  • 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-10T15:06:36+00:00Added an answer on June 10, 2026 at 3:06 pm

    You don’t say which version of .NET you’re using. In versions prior to 4.0, GZipStream compresses data on a per-write basis. That is, it compresses the buffer you send to it. In your first example, the Serialize method is likely writing very small buffers to the stream (one field at a time). In your second example, Serialize serializes the entire object to the memory stream, and then the memory stream’s buffer is written to the GZipStream in one big chunk. GZipStream does much better when it has a larger buffer (64K is close to optimum) to work with.

    This may still be the case in .NET 4.0. I don’t remember if I tested it.

    The way I’ve handled this in the past is with a BufferedStream:

    using (var mstream = new MemoryStream())
    {
        using (var bstream = new BufferedStream(new GZipStream(mstream, CompressionMode.Compress), 65536))
        {
            new BinaryFormatter().Serialize(btream, obj);
        }
        return mstream.ToArray();
    }
    

    That way, the compressor gets a 64K buffer to work with.

    Prior to .NET 4.0, there was no benefit to providing a buffer larger than 64K for GZipStream. I’ve seen some information indicating that the compressor in .NET 4.0 can do a better job of compression with a larger buffer. However, I’ve not tested that myself.

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

Sidebar

Related Questions

This is probably something silly I'm missing but I'm definitely lost. I'm using .NET
I'm definitely missing something. I have an XML file that stores HTML snippets, as
I am completely lost. I think I am definitely missing something fundamental here. Everybody
Having a heckuva time with this one, though I feel I'm missing something obvious.
Total Python noob here, probably missing something obvious. I've searched everywhere and haven't found
Okay this is definitely a n00b question but here goes. The way I understand
I've read all the posts on Stack Overflow about CreateProcessAsUser and there are very
Can someone explain to me in, in REST terms, Twitter's design decision with the
I'm definitely a newbie to ruby (and using 1.9.1), so any help is appreciated.
I have the following code which definitely returns a proper data result if I

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.