This question is locked. New answers and comments are not allowed.
Hello everybody!
I've got question about memory leaks.
I've got following code:
DF class (alias) is the following:
ReadFullyStream is the following:
After I trying to upload several times of file with 100mb size I get OutOfMemoryException. I tried to profile with CLRProfiler and it seems that OA pooling something wich holds reference to this huge byte[] and prevent it from GC collect. What I'm doing wrong?
If needed I will provide more info.
Thanks in advance.
I've got question about memory leaks.
I've got following code:
| [OperationBehavior(ReleaseInstanceMode = ReleaseInstanceMode.AfterCall)] |
| public void AddDataFile(DataFileMessage userDataFile) |
| { |
| Console.WriteLine("Allocated memory before is = {0}M",GC.GetTotalMemory(false)/1000000); |
| if (userDataFile == null) throw new ArgumentNullException("userDataFile"); |
| if (userDataFile.FileToStore == null) throw new ArgumentNullException("stream is null"); |
| IObjectScope scope = ArchivariusModelProvider.GetNewObjectScope(); |
| if (scope != null) |
| { |
| var query = from appgroup in scope.Extent<ApplicationGroup>() |
| where appgroup.GroupId == userDataFile.GroupId |
| select appgroup; |
| Debug.Assert(query.Count() == 1); |
| DF dFile = new DF |
| { |
| FileName = userDataFile.FileName, |
| DataFiles = userDataFile.FileToStore.ReadFullyStream(), |
| ApplicationGroup = query.First() |
| }; |
| scope.Transaction.Begin(); |
| scope.Add(dFile); |
| scope.Transaction.Commit(); |
| scope.Dispose(); |
| } |
| } |
DF class (alias) is the following:
| public partial class DataFile |
| { |
| //The 'no-args' constructor required by OpenAccess. |
| public DataFile() |
| { |
| DataFilesId = Guid.NewGuid(); |
| } |
| [Telerik.OpenAccess.FieldAlias("dataFilesId")] |
| public Guid DataFilesId |
| { |
| get { return dataFilesId; } |
| set { dataFilesId = value; } |
| } |
| [Telerik.OpenAccess.FieldAlias("dataFiles")] |
| public byte[] DataFiles |
| { |
| get { return dataFiles; } |
| set { this.dataFiles = value; } |
| } |
| [Telerik.OpenAccess.FieldAlias("groupId")] |
| public int GroupId |
| { |
| get { return groupId; } |
| set { this.groupId = value; } |
| } |
| [Telerik.OpenAccess.FieldAlias("applicationGroup")] |
| public ApplicationGroup ApplicationGroup |
| { |
| get { return applicationGroup; } |
| set { this.applicationGroup = value; } |
| } |
| [Telerik.OpenAccess.FieldAlias("fileName")] |
| public string FileName |
| { |
| get { return fileName; } |
| set { fileName = value; } |
| } |
| } |
| public static byte[] ReadFullyStream(this Stream stream) |
| { |
| if (stream == null) throw new ArgumentNullException("stream"); |
| byte[] buffer = new byte[32768]; |
| using (MemoryStream ms = new MemoryStream()) |
| { |
| while (true) |
| { |
| int read = stream.Read(buffer, 0, buffer.Length); |
| if (read <= 0) |
| { |
| return ms.ToArray(); |
| } |
| ms.Write(buffer, 0, read); |
| } |
| } |
| } |
After I trying to upload several times of file with 100mb size I get OutOfMemoryException. I tried to profile with CLRProfiler and it seems that OA pooling something wich holds reference to this huge byte[] and prevent it from GC collect. What I'm doing wrong?
If needed I will provide more info.
Thanks in advance.