0

SCENARIO I'm using C# Webforms with .NET framework 2.0* (the project is 3.5, but the IIS has only 2.0). I have a simple form that gets some user's information and files (PDF, gif, etc) and send them to another application. Pretty simple. The problem is that eventually there's A LOT of people (over 1000) trying to access it at the very same time and the memory usage of the IIS process explodes, causing the application to crash. We're trying to improve the infra (more memory, webfarm, etc), but I'd like to know:

QUESTIONS

  1. Do I really NEED to manually call FileUpload.FileContent.Close() and FileUpload.FileContent.Dispose() like in the finally clause below? Can it help solving the aforementioned problem? ==> ANSWERED: YES

  2. If I do so, can it mess up the cleanup process of the temporary files used by the framework? ==> ANSWERED: NO

  3. What else can I do to avoid this problem? For now, I'll try changing the Webconfig following these articles: http://support.microsoft.com/kb/821268/en-us AND http://www.codeproject.com/Articles/23306/ASP-NET-Performance-and-Scalability-Secrets

CODE Here's the code fragment where I'm using the FileUpload.SaveAs(). It basically gets the generated and already validated info (filename, extension, etc) and uses it to save the fileContent of the fileupload.

    private bool GravarArquivo(FileUpload fileUpload, eXML.Anexo anexo)
    {
        bool sucesso = false;
        if (AnexoValido(fileUpload, anexo))
        {
            string path = Server.MapPath(bllProcesso.GetUrlArquivo(anexo.NomeArquivoFisico));
            try
            {
                //GRAVA ARQUIVO
                fileUpload.SaveAs(path);
                sucesso = true;
            }
            catch (Exception ex)
            {
                string mensagem = String.Format("Erro ao tentar salvar o arquivo '{0}'. Exception: '{1}'", anexo.NomeArquivoFisico, ex.Message);
                MostrarAlerta(this, mensagem);
                //throw new Exception(mensagem, ex);
            }
            //TODO: ESTUDAR O USO DESTE FINALLY, PARA VER SE DIMINUI O PROBLEMA DE MEMÓRIA DO SERVIDOR
            //finally
            //{
            //    fileUpload.FileContent.Close();
            //    fileUpload.FileContent.Dispose();
            //}
        }
        return sucesso;
    }
erick2711
  • 13
  • 3
  • Short answer, yes. See [this question](http://stackoverflow.com/questions/16601929/dispose-for-cleaning-up-managed-resources) – Joe May 16 '14 at 15:30
  • @Joe - tahnks. But how about the temp file of the FileUpload? Won't this mess up the cleanup process? – erick2711 May 16 '14 at 17:26

1 Answers1

0

You should always call Dispose when finished using a resource. This frees it's internal resources for other applications to use.

Calling Dispose does NOT free up memory.

If you do not call Dispose, it will be called automatically at some indeterminate time in the future.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • Thanks for the answer. But CAN I really call the FileUpload.FileContent.Dispose()? I don't really know how the framework handles the FileUpload. I know there's a temp file somewhere, and the FileUpload.FileContent.SaveAs() just copy it to the desired location. But if I call this Dispose() wont I mess up the process of cleaning the temp file, whathever is this process? – erick2711 May 16 '14 at 17:06
  • Yes you can safely call Dispose. The operating system handles Temp files, not .NET – Steve Wellens May 17 '14 at 02:07
  • thank you, sir. By the way, do you have any other advise about what to do in the software to minimize the described problem? – erick2711 May 19 '14 at 17:21