关于序列化的测试

来源:互联网 发布:php源代码是什么 编辑:程序博客网 时间:2024/06/06 23:18

表:

1 1990-01-01 00:00:00.000 0.73
2 1990-01-02 00:00:00.000 0.71

....

共21914 条记录

 

 

今天试了下DataTable序列化后的信息:(gzip:GZipStream)

Binary: org:394,179                  gzip:244,806

XML:     org:3,737,190              gzip:339,290

 

 

SQL Management Studio:

(21914 row(s) affected)

TDS packets received from server: 108

Bytes received from server: 439210

 

 

 

今天测了下序列化一个178M的文件(先将文件读到MemoryStream,然后再序列化),在读文件至MemoryStream时就出现:OutOfMemory错误,有时是序列化时出错。

担心Web请求不能发过多内容,于是使用下面的代码做了下测试:

        public static void SerializeToWebStream(FileStream fsOrg)
        {
            fsOrg.Position = 0;
            string strFileName = Path.GetTempFileName();
            using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress))
                {
                    int readCnt = 0;
                    byte[] buff = new byte[BufferSize];
                    do
                    {
                        readCnt = fsOrg.Read(buff, 0, BufferSize);
                        zipStream.Write(buff, 0, readCnt);
                    } while (readCnt > 0);
                    zipStream.Flush();
                    fs.Flush();
                }

            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";

            using (FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.ReadWrite))
            {
                HttpContext.Current.Response.AppendHeader("Content-Length", fs.Length.ToString());
                fs.Position = 0;
                int readCnt = 0;
                byte[] buff = new byte[BufferSize];
                do
                {
                    readCnt = fs.Read(buff, 0, BufferSize);
                    HttpContext.Current.Response.OutputStream.Write(buff, 0, readCnt);
                    HttpContext.Current.Response.Flush();
                } while (readCnt > 0);
            }

        }

 178M的文件是可以发至客户端的。

 

看来读大文件至MemoryStream,或者是序列化大对象至MemoryStream要小心了。

建议使用temp文件来缓冲结果。

 

 

原创粉丝点击