Skip to content

Fix memory leak in TensorflowTransform #4223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Microsoft.ML.Dnn/DnnUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ public Runner AddInput(Tensor value, int index)
return this;
}

public List<Tensor> GetInputValues()
{
return _inputValues;
}

public Runner AddOutputs(string output)
{
_outputs.Add(ParseOutput(output));
Expand Down
13 changes: 12 additions & 1 deletion src/Microsoft.ML.TensorFlow/TensorflowTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ private void Dispose(bool disposing)
{
Session.close(); // invoked Dispose()
}

if (Session != null && Session.graph != IntPtr.Zero)
{
Session.graph.Dispose();
}
}
finally
{
Expand Down Expand Up @@ -645,7 +650,7 @@ private void UpdateCacheIfNeeded(long position, ITensorValueGetter[] srcTensorGe
Runner runner = new Runner(_parent.Session);

// Feed inputs to the graph.
for (int i = 0; i < _parent.Inputs.Length; i++)
for (int i = 0; i < _parent.Inputs.Length; i++)
{
var tensor = srcTensorGetters[i].GetTensor();
runner.AddInput(_parent.Inputs[i], tensor);
Expand All @@ -658,6 +663,12 @@ private void UpdateCacheIfNeeded(long position, ITensorValueGetter[] srcTensorGe
// Execute the graph.
var tensors = runner.Run();

List<Tensor> inputTensors = runner.GetInputValues();
foreach (Tensor inputTensor in inputTensors)
{
inputTensor.Dispose();
}

Contracts.Assert(tensors.Length > 0);

for (int j = 0; j < activeOutputColNames.Length; j++)
Expand Down