2014-04-17 63 views
0

我做了一個服務器和客戶端(作爲培訓)。他們都溝通完美。當Iam從我的客戶端上傳文件到我的服務器時,出現唯一的問題。當服務器正在下載我的服務器時 - RAM大約上升860MB( - >下載299MByte文件)。我注意到,每次客戶端上傳我的RAM都不會回到正常值。相反,它被添加到我的服務器當前的RAM值(2個客戶端上傳完畢 - >服務器RAM越過1300MB)下載文件後RAM不增加到正常值

這是從我的服務器我的代碼在那裏reciving文件

private void DownloadFileFromClient(string path, string nameAndExtension, TcpClient client) 
    { 


     string currFileName = "Download"; 
     string savingPath = path; 

     if (File.Exists(savingPath)) //Wenn der Pfad einen Namen für die Datei enthält 
     { 
      FileInfo fi = new FileInfo(savingPath); 
      currFileName = fi.Name; 
     } 
     else if (Directory.Exists(savingPath)) //Wenn der Pfad zu einem Ordner führt 
     { 
      int counter = 0; //zählt die doppelten Datein nachoben 
      foreach (string file in Directory.GetFiles(savingPath)) 
      { 
       FileInfo fi = new FileInfo(file); 
       string[] name = fi.Name.ToString().Split('.'); 
       if (name[0].ToUpperInvariant() == currFileName.ToUpperInvariant()) //Wenn schon eine Datei mit dem selben Namen vorhanden ist 
       { 
        counter++; 
        currFileName = "download" + counter.ToString(); 
       } 

      } 

      savingPath = savingPath + "\\" + currFileName + "." + nameAndExtension; 
     } 


     using (NetworkStream stream = client.GetStream()) 
     { 
      BinaryFormatter bf = new BinaryFormatter(); 
      object op; 
      op = bf.Deserialize(stream); // Deserialize the Object from Stream 

      BinaryReader br = new BinaryReader(stream); 
      byte[] buffer = br.ReadBytes(MaxDownloadBytes); //Maximale Dateiengröße in Bytes 10MB = 10485760Bytes | 50MB = 52428800Bytes | 100MB = 104857600Bytes | 500MB = 524288000Bytes 
      br.Dispose(); 
      br.Close(); 

      using (FileStream filestream = new FileStream(savingPath, FileMode.CreateNew, FileAccess.Write)) 
      { 
       filestream.Write(buffer, 0, buffer.Length); 
      } 
     } 
     LogMessage("Succefully downloaded file from Client", client.GetHashCode().ToString(), "Manuel", "test", client); 


    } 

我嘗試了全部處理,但它並沒有幫助很多。謝謝你的回答!

+0

使用探查器並快樂地檢查你的堆。使用這樣的工具,您可以非常快速地識別泄漏。 –

+0

是一個需求的快樂部分? – Jonesopolis

回答

0

我解決了...差不多。文件流關閉後,我剛剛添加了GC.Collect();

(codesource的從TyCobb,感謝)

using (NetworkStream stream = client.GetStream()) 
{ 
    using(BinaryReader br = new BinaryReader(stream)) 
    { 
     byte[] buffer = br.ReadBytes(MaxDownloadBytes); 

     using (FileStream filestream = new FileStream(savingPath, FileMode.CreateNew, FileAccess.Write)) 
     { 
      filestream.Write(buffer, 0, buffer.Length); 
     } 

     GC.Collect(); 
    } 
} 

但299MB後不知何故下載RAM高達300MB,但如果IAM下載並沒有改變另一個文件。

+0

如果它只是一個GC.Collect,那麼沒有任何問題,系統會在需要時釋放內存。沒有理由調用GC.Collect。你仍然應該考慮分塊,所以它甚至不會佔用那麼多的內存。 – TyCobb

1

我並不一定會看到任何錯誤,但所有流都可能存在一些不可思議的情況,並且您正在閱讀的內容不一定有用。下面是你必須刪除看起來不必要的代碼/流閱讀的一個調整。

using (NetworkStream stream = client.GetStream()) 
{ 
    using(BinaryReader br = new BinaryReader(stream)) 
    { 
     byte[] buffer = br.ReadBytes(MaxDownloadBytes); 

     using (FileStream filestream = new FileStream(savingPath, FileMode.CreateNew, FileAccess.Write)) 
     { 
      filestream.Write(buffer, 0, buffer.Length); 
     } 
    } 
} 

我強烈建議這樣做的大塊。這樣做可以讓您刪除BinaryReader的必要性。 You can see an example of what just using the NetworkStream looks like here。這也會讓你的下載消耗更少的內存,因爲在你編寫它之前你並沒有把整個文件下載到內存中。

+0

感謝您的回答。我將在星期二進行測試。 – C0d1ngJammer

+0

它stil不起作用。而且我不知道如何解決它 – C0d1ngJammer