Search code examples
asp.netvb.netapplication-pool

ZipOutputStream MemoryLeak


I am using ZipOutputStream to get files from db, compress to zip and download (zip file around 400mb). During this, my app pool memory is going up to 1.4gb and after download is complete, its coming down to 1gb when it should come back to like 100 mb or something. There are only like 10 users using this app and only 1 user using this particular page. i am calling the dispose method. I aslo tried explictly calling GC.Collect but still no use. Am i missing anything here?

Thanks in advance.

  Dim zipStream = New ZipOutputStream(HttpContext.Current.Response.OutputStream)
    Try


        da.Fill(ds)
        For Each dr As DataRow In ds.Tables(0).Rows
            Try
                Dim docName As String = ""
                strImgID = dr("image_id")
                If Not IsDBNull(dr("loan_number")) Then iLoanID = dr("loan_number")

                If Not IsDBNull(dr("document_name")) Then docName = dr("document_name")
                Dim ext As String = dr("image_type_extension")
                Dim strFinalFileName As String = ""

                strFinalFileName = docName & "_" & iLoanID & ext

                Dim b As Byte() = dr("image_binary")
                Dim fileEntry = New ZipEntry(Path.GetFileName(strFinalFileName))
                zipStream.PutNextEntry(fileEntry)
                zipStream.Write(b, 0, b.Length)
            Catch ex As Exception
                LogError(ex, iLoanID & "," & strImgID)
                AddError(sb, ex, iLoanID & "," & strImgID)
            End Try
        Next

     Catch ex As Exception
        Throw
    Finally

        zipStream.Close()
        zipStream.Dispose()
          cmd.Connection.Close()
        cmd.Connection.Dispose()
    End Try

Solution

  • You need to chunk data into the stream rather than allocate all at once.

    E.g. (in c#)

    byte[] buffer = new byte[4096];
    
    FileStream readFs = File.OpenRead(strFile);
    
    for (int rnt = readFs.Read(buffer, 0, buffer.Length); 
          rnt > 0; 
          rnt = readFs.Read(buffer,  0, buffer.Length))
    {
          zipoutputstream.Write(buffer, 0, rnt);
    }
    

    I think this will help with your memory issue. Please comment back if not..