quarta-feira, 26 de abril de 2017

Avoid Memory Leaks C# Chunk Base64Strings 1GB or more.



#region Read For Chunks
                    string filename = fileTmp + ".json.js";
                    var file = File.CreateText(filename);
                    file.Write(bigFILESTRING());
                    file.Close();
                    GC.Collect();
                  
                    if (true)
                    {
                        var fsOpen = new FileStream(filename, FileMode.Open);
                        var fs = new FileStream(fileTmp, FileMode.Create);

                        BinaryReader rdr = new BinaryReader(fsOpen);
                        BinaryWriter bw = new System.IO.BinaryWriter(fs);

                        int streamLength = (int)rdr.BaseStream.Length;
                        int chunkSize = 8;
                        try
                        {
                            while (rdr.BaseStream.Position < streamLength)
                            {
                                byte[] b = new byte[chunkSize];

                                long remaining = streamLength - rdr.BaseStream.Position;
                                if (remaining >= chunkSize)
                                {
                                    rdr.Read(b, 0, chunkSize);
                                }
                                else
                                {
                                    rdr.Read(b, 0, (int)remaining);
                                }


                                string result = System.Text.Encoding.UTF8.GetString(b);


                                if (IsBase64(result))
                                    bw.Write(Convert.FromBase64String(result));
                                else
                                    break;


                            }
                        }
                        finally
                        {
                            bw.Close();
                            fs.Close();
                            fsOpen.Close();
                        }
                    }

  #endregion


wiki.openstreetmap.org/wiki/Admin_level
Postar um comentário