Skip to content

Commit

Permalink
fix leak memory issue in snap creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mimetis committed Nov 29, 2021
1 parent 4b783e3 commit 3d6f76c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
6 changes: 5 additions & 1 deletion Projects/Dotmim.Sync.Core/Batch/BatchPartInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ internal static async Task<BatchPartInfo> CreateBatchPartInfoAsync(int batchInde
// The batch part creation process will serialize the changesSet to the disk

// Serialize the file !
await SerializeAsync(set.GetContainerSet(), fileName, directoryFullPath, serializerFactory, orchestrator);
var containerSet = set.GetContainerSet();
await SerializeAsync(containerSet, fileName, directoryFullPath, serializerFactory, orchestrator);

containerSet.Dispose();
containerSet = null;

bpi = new BatchPartInfo { FileName = fileName };
bpi.Index = batchIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task RunAsync(T args, CancellationToken cancellationToken)
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
//GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool cleanup) => this.wrapperAsync = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ public virtual Task<BatchInfo> CreateSnapshotAsync(SyncParameters syncParameters
if (bptis != null)
{
// Statistics
var tableChangesSelected = new TableChangesSelected(table.TableName, table.SchemaName);

// we are applying a snapshot where it can't have any deletes, obviously
tableChangesSelected.Upserts = bptis.Sum(bpti => bpti.RowsCount);
var tableChangesSelected = new TableChangesSelected(table.TableName, table.SchemaName)
{
// we are applying a snapshot where it can't have any deletes, obviously
Upserts = bptis.Sum(bpti => bpti.RowsCount)
};

if (tableChangesSelected.Upserts > 0)
changesSelected.TableChangesSelected.Add(tableChangesSelected);
Expand Down Expand Up @@ -260,7 +261,7 @@ internal virtual async Task<BatchInfo> InternalCreateSnapshotAsync(SyncContext c
var row = this.CreateSyncRowFromReader(dataReader, changesSetTable);

// Add the row to the changes set
changesSetTable.Rows.Add(row);
changesSetTable.Rows.Add(row);

// Set the correct state to be applied
if (row.RowState == DataRowState.Deleted)
Expand Down Expand Up @@ -292,7 +293,8 @@ internal virtual async Task<BatchInfo> InternalCreateSnapshotAsync(SyncContext c
batchIndex++;

// we know the datas are serialized here, so we can flush the set
changesSet.Clear();
changesSet.Dispose();
changesSetTable.Dispose();

// Recreate an empty ContainerSet and a ContainerTable
changesSet = new SyncSet();
Expand All @@ -301,9 +303,12 @@ internal virtual async Task<BatchInfo> InternalCreateSnapshotAsync(SyncContext c

// Init the row memory size
rowsMemorySize = 0L;

GC.Collect();
}

dataReader.Close();
GC.Collect();

// We don't report progress if no table changes is empty, to limit verbosity
if (tableChangesSelected.Deletes > 0 || tableChangesSelected.Upserts > 0)
Expand Down
2 changes: 1 addition & 1 deletion Projects/Dotmim.Sync.Core/Set/ContainerSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Dispose()
{
this.Dispose(true);

GC.SuppressFinalize(this);
//GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool cleanup)
Expand Down
2 changes: 1 addition & 1 deletion Projects/Dotmim.Sync.Core/Set/SyncFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public override bool EqualsByProperties(SyncFilter other)
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
//GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool cleanup)
Expand Down
11 changes: 10 additions & 1 deletion Projects/Dotmim.Sync.Core/Set/SyncSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void Dispose()
if (this.Filters != null)
this.Filters.Schema = null;

GC.SuppressFinalize(this);
//GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool cleanup)
Expand All @@ -168,13 +168,22 @@ protected virtual void Dispose(bool cleanup)
if (cleanup)
{
if (this.Tables != null)
{
this.Tables.Clear();
this.Tables = null;
}

if (this.Relations != null)
{
this.Relations.Clear();
this.Relations = null;
}

if (this.Filters != null)
{
this.Filters.Clear();
this.Filters = null;
}
}

// Dispose unmanaged ressources
Expand Down
8 changes: 7 additions & 1 deletion Projects/Dotmim.Sync.Core/Set/SyncTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void EnsureTable(SyncSet schema)
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
//GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool cleanup)
Expand All @@ -132,10 +132,16 @@ protected virtual void Dispose(bool cleanup)
if (cleanup)
{
if (this.Rows != null)
{
this.Rows.Clear();
this.Rows = null;
}

if (this.Columns != null)
{
this.Columns.Clear();
this.Columns = null;
}

this.Schema = null;
}
Expand Down
2 changes: 1 addition & 1 deletion Projects/Dotmim.Sync.Core/SyncAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ await Task.Run(async () =>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
//GC.SuppressFinalize(this);
}

/// <summary>
Expand Down

0 comments on commit 3d6f76c

Please sign in to comment.