c# - Get serialised size of List without having OutOfMemoryException -


ok clear, i'm aware loh thing , big object (more 85k) , big list (more 40k elements) gets loh.

so problem is, i need know size of list when serialised using xmlserialiser (i don't care space takes in ram or that, want size when serialized) if try serialise big list containing big elements outofmemoryexception (and know why)

what want know : possible serialise list element element , cumulate size of in loop : //this pseudo code

long bytelength = 0; using(stream) {   foreach(element in mylist)   {     memorystream.serialise(element);     bytelength += memorystream.length;      memorystream.clear();   } } 

any suggestion ?


update : solution @xanatos want because doesn't add in ram big byte[] stored in loh

as @hans passant said, seems purpose of why want treatment important : want know size in byte of list serialised in xml able split list in multiple file on disk according total byte.

if need length:

class nulstream : stream {     public override bool canread     {         { return false;  }     }      public override bool canseek     {         { return false; }     }      public override bool canwrite     {         { return true; }     }      public override void flush()     {     }      protected long length;      public override long length     {         { return this.length; }     }      public override long position     {                 {             return this.length;         }         set         {             throw new notsupportedexception();         }     }      public override int read(byte[] buffer, int offset, int count)     {         throw new notsupportedexception();     }      public override long seek(long offset, seekorigin origin)     {         throw new notsupportedexception();     }      public override void setlength(long value)     {         throw new notsupportedexception();     }      public override void write(byte[] buffer, int offset, int count)     {         this.length += count;     } }  using (var nul = new nulstream()) {     xml.serialize(nul, lst);     long length = nul.length; } 

it nul stream... nul file :-) eat throw @ it, , save cumulative length.

note technically have implemented setlength, , write should check parameters... why? :-)


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -