. Net binaryformatter causes slow Object serialization

Source: Internet
Author: User
Tags object serialization
ArticleDirectory
    • Protobuf.net test code
    • Binaryformatter test code
    • Memory usage of protobuf.net
    • Memory usage of binaryformatter

We have recently optimized the stream Writing of component objects. the serialization component under. net has been tested for protobuf.net and. net binaryformatter is analyzed. according to the test results, the performance of binaryformatter is 10 times different from that of protobuf.net. Why is the difference so far? If we look closely at the running time, we may think that binaryformatter must use reflection or something, so the result is so slow. In order to better understand the specific situation, we will test the two.CodeA memory analysis is performed.

Protobuf.net test code
Public void Pb (INT count) {testpb OBJ = new testpb (); obj. email = "henryfan@test.com"; obj. firstname = "Henry"; obj. lastname = "fan"; obj. id = 3456; obj. phone = "13418888121"; obj. type = 67; system. io. memorystream stream = new system. io. apsaradb for memorystream (265); stream. position = 0; protobuf. serializer. serialize <testpb> (stream, OBJ); system. diagnostics. stopwatch Sw = new system. diagnostics. stopwatch (); Sw. reset (); Sw. start (); For (INT I = 0; I <count; I ++) {stream. position = 0; protobuf. serializer. serialize <testpb> (stream, OBJ);} SW. stop (); console. writeline ("protobuf serialize objects {0} Time: {1}", Count, SW. elapsed. totalmilliseconds );}
Binaryformatter test code
 Public void DOTNET (INT count) {testdotnet OBJ = new testdotnet (); obj. email = "henryfan@test.com"; obj. firstname = "Henry"; obj. lastname = "fan"; obj. id = 3456; obj. phone = "13418888121"; obj. type = 67; system. io. memorystream stream = new system. io. memorystream (256); binaryformatter BF = new binaryformatter (); stream. position = 0; BF. serialize (stream, OBJ); system. diagnostics. stopwatch Sw = new system. diagnostics. stopwatch (); Sw. reset (); Sw. start (); For (INT I = 0; I 
  

Use memory analysis to view the memory overhead of 100000 serialized objects for the above Code

Memory usage of protobuf.net
Name Number of non-exclusive allocations Exclusive allocation count Number of non-exclusive bytes Exclusive bytes Percentage of non-exclusive Distributions
+ Protobuf. protowriter 100,001 100,001 6,000,060 6,000,060 24.44
+ Protobuf. netobjectcache 100,001 100,001 2,400,024 2,400,024 24.44
+ Protobuf. Meta. runtimetypemodel. typefinder 200,002 200,002 2,400,024 2,400,024 48.88
+ System. byte [] 1,199 1,199 2,111,060 2,111,060 0.29
+ System. String 697 697 32,418 32,418 0.17
+ System. Char [] 44 44 25,382 25,382 0.01
+ System. object [] 350 350 20,300 20,300 0.09
+ System. Collections. Generic. List '1 589 589 14,136 14,136 0.14
+ System. reflection. emit. opcode 226 226 9,944 9,944 0.06
+ System. reflection. methodinfo [] 256 256 9,012 9,012 0.06
+ System. reflection. runtimemethodinfo 123 123 6,888 6,888 0.03
+ System. Collections. arraylist 259 259 6,216 6,216 0.06
Memory usage of binaryformatter
Name Number of non-exclusive allocations Exclusive allocation count Number of non-exclusive bytes Exclusive bytes Percentage of non-exclusive Distributions
+ System. Collections. hashtable. Bucket [] 500,008 500,008 72,001,656 72,001,656 8.77
+ System. object [] 600,012 600,012 39,204,636 39,204,636 10.52
+ System. byte [] 201,074 201,074 31,699,205 31,699,205 3.53
+ System. Collections. hashtable 500,008 500,008 28,000,448 28,000,448 8.77
+ System. runtime. serialization. formatters. Binary. nameinfo 400,004 400,004 19,200,192 19,200,192 7.01
+ System. String 100,078 100,078 19,005,036 19,005,036 1.75
+ System. int64 [] 100,001 100,001 17,200,172 17,200,172 1.75
+ System. runtime. serialization. formatters. Binary. objectwriter 100,001 100,001 10,400,104 10,400,104 1.75
+ System. runtime. serialization. formatters. Binary. _ binarywriter 100,001 100,001 10,000,100 10,000,100 1.75
+ System. runtime. serialization. formatters. Binary. writeobjectinfo 100,001 100,001 6,800,068 6,800,068 1.75
+ System. runtime. serialization. formatters. Binary. serstack 200,002 200,002 4,800,048 4,800,048 3.51
+ System. runtime. serialization. formatters. Binary. binaryobjectwithmaptyped 100,001 100,001 4,400,044 4,400,044 1.75

the above two memory analysis results show that protobuf.net only consumes about 13 MB of memory during serialization, and the total number of created objects is About 400000; in contrast, the binaryformatter uses more than 300 MB of memory during the serialization process, and the total number of created objects is close to 6000000. The closeness is that the number of objects created is 10 times that of protobuf.net, so the efficiency is normal. Why does Ms do this? Obviously, it can do well, but it does not ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.