Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. BinaryFormatter - Serialize OutOfMemory Exception

BinaryFormatter - Serialize OutOfMemory Exception

Scheduled Pinned Locked Moved C#
jsonperformancehelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kit Fisto
    wrote on last edited by
    #1

    Hello guys, I'm having a problem and, although I understand the problem, I can't find a solution to the problem. I'm using NamedPipes and I have created this method to Serialise on object T to a byte[].

        private byte\[\] Serialize(T obj)
        {
            using (var memoryStream = new MemoryStream())
            {
                oBinaryFormatter.Serialize(memoryStream, obj);
    
                return memoryStream.ToArray();
            }
        }
    

    I have a test that sends a object with 300Mb and the Serialization fails with a OutOfmemory.

    oBinaryFormatter.Serialize(memoryStream, 300MbObject);

    I understand that the problem is related to memory. Can you guys suggest another solution for this problem? Thanks Guys :)

    S L 2 Replies Last reply
    0
    • K Kit Fisto

      Hello guys, I'm having a problem and, although I understand the problem, I can't find a solution to the problem. I'm using NamedPipes and I have created this method to Serialise on object T to a byte[].

          private byte\[\] Serialize(T obj)
          {
              using (var memoryStream = new MemoryStream())
              {
                  oBinaryFormatter.Serialize(memoryStream, obj);
      
                  return memoryStream.ToArray();
              }
          }
      

      I have a test that sends a object with 300Mb and the Serialization fails with a OutOfmemory.

      oBinaryFormatter.Serialize(memoryStream, 300MbObject);

      I understand that the problem is related to memory. Can you guys suggest another solution for this problem? Thanks Guys :)

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #2

      Don't use the BinaryFormatter for anything that size. I would recommend either using protobuf.net[^] or manual serializing: BinaryFormatter vs. Manual Serializing[^]. Both will be faster and more compact.

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      B 1 Reply Last reply
      0
      • S Sascha Lefevre

        Don't use the BinaryFormatter for anything that size. I would recommend either using protobuf.net[^] or manual serializing: BinaryFormatter vs. Manual Serializing[^]. Both will be faster and more compact.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        B Offline
        B Offline
        Brisingr Aerowing
        wrote on last edited by
        #3

        Since Google Code is shutting down, you may want to direct people to the correct homepage of protobuf.net[^] on Github.

        What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

        S 1 Reply Last reply
        0
        • K Kit Fisto

          Hello guys, I'm having a problem and, although I understand the problem, I can't find a solution to the problem. I'm using NamedPipes and I have created this method to Serialise on object T to a byte[].

              private byte\[\] Serialize(T obj)
              {
                  using (var memoryStream = new MemoryStream())
                  {
                      oBinaryFormatter.Serialize(memoryStream, obj);
          
                      return memoryStream.ToArray();
                  }
              }
          

          I have a test that sends a object with 300Mb and the Serialization fails with a OutOfmemory.

          oBinaryFormatter.Serialize(memoryStream, 300MbObject);

          I understand that the problem is related to memory. Can you guys suggest another solution for this problem? Thanks Guys :)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          A typical object is not 300Mb; that already counts as a rather large transfer of data. Do you need to have it completely in memory? If it represents "data", would it not be more logical to stream the data straight to file, as opposed to creating a very large object in memory? The memory manager really does not like large objects. I'd recommend to cut your block into ten chuncks. If you have to stream it to a memorystream, then at least allocate it once and with the correct size - otherwise it may need be reallocated and cause fragmentation.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          K 1 Reply Last reply
          0
          • B Brisingr Aerowing

            Since Google Code is shutting down, you may want to direct people to the correct homepage of protobuf.net[^] on Github.

            What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #5

            Right - forgot about that. Thank you for the heads-up.

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            1 Reply Last reply
            0
            • L Lost User

              A typical object is not 300Mb; that already counts as a rather large transfer of data. Do you need to have it completely in memory? If it represents "data", would it not be more logical to stream the data straight to file, as opposed to creating a very large object in memory? The memory manager really does not like large objects. I'd recommend to cut your block into ten chuncks. If you have to stream it to a memorystream, then at least allocate it once and with the correct size - otherwise it may need be reallocated and cause fragmentation.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              K Offline
              K Offline
              Kit Fisto
              wrote on last edited by
              #6

              Thx guys for your replys. I went with the solution of cutting the objects into chuncks ;)

              L 1 Reply Last reply
              0
              • K Kit Fisto

                Thx guys for your replys. I went with the solution of cutting the objects into chuncks ;)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You're welcome :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups