Добрый день, возникла проблема с использованием памяти приложением, когда происходит передача файла (2 Мб) на сервер. Размер приложения в памяти из 102 Мб превращается в 1.2 Гб, что, собственно, проблема. Не могу понять почему не происходит очистка ресурсов.
Здесь создается класс контракта данных
[Serializable]
[DataContract(Name = "ExecFile")]
[KnownType(typeof(System.Management.ManagementObjectSearcher))]
public class ExecFile // класс c файлом
{
[DataMember(Name = "File")]
public byte[] File = new byte[40000000];
[DataMember(Name = "FileName")]
public string FileName = "";
}
Только здесь он используется
public void SendDataSettingPC(string ParameterType, string ParameterRequest, int PositionData) // Выполняет комманды сервера
{
string Result = "";
string FileName = "";
lock (ValueWCFRequest.MainChannelSending)
{
if (ParameterRequest != "" && File.Exists(ParameterRequest))
{
ValueWCFRequest.MainAddressEndepont = new EndpointAddress("http://" + ValueWCFConnect.WCFServer_IP + ":" + ValueWCFConnect.WCFServer_Port + "/AdministrationServiceManagement/MainEndpoint");
ValueWCFRequest.MainChannelSending = new ChannelFactory<IWCFService>(ValueWCFRequest.MainConfigProtocol, ValueWCFRequest.MainAddressEndepont);
try
{
IWCFService WCFClientOpen = ValueWCFRequest.MainChannelSending.CreateChannel();
using (FileStream CreatArray = new FileStream(ParameterRequest, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
ExecFile SetData = new ExecFile();
XmlSerializer XmlData = new XmlSerializer(typeof(ExecFile));
byte[] DataFile = new byte[40000000];
int Leng = (int)CreatArray.Length;
int CountCell;
int SumCell = 0;
while ((CountCell = CreatArray.Read(DataFile, SumCell, Leng - SumCell)) > 0)
{
SumCell += CountCell; // опеределяем необходимое количество строк в байтовом массиве путем смещения
}
CreatArray.Close();
CreatArray.Dispose();
FileName = Path.GetFileName(ParameterRequest);
SetData.FileName = FileName;
Array.Copy(DataFile, SetData.File, SumCell);
using (StringWriter Stream = new StringWriter())
{
XmlData.Serialize(Stream, SetData);
Result = WCFClientOpen.MainMethodsInternal(ParameterType, Stream.ToString());
}
Array.Clear(SetData.File, 0, SetData.File.Length);
}
ValueWCFRequest.MainChannelSending.Close();
GC.Collect();
}
catch (Exception Error)
{
SettingPC.BasicMethods.ValueSetting.TypeSourceData = "localhost";
MessageBox.Show("Не удалось связаться с сервером " + Error.Message, "Ошибка cоединения", MessageBoxButtons.OK, MessageBoxIcon.Error);
GC.Collect();
}
}
else return;
}
Использовал сборщик мусора. Да, он помогает, но с 1.2 Гб до 412 Мб, что всё равно многовато. Да и почему он вообще резервирует такой объем?