Skip to content

Commit

Permalink
Add a memory pressure hint for NewFromMemoryCopy
Browse files Browse the repository at this point in the history
To ensure that the GC knows the true cost of the image during collection.
  • Loading branch information
kleisauke committed Jan 29, 2020
1 parent 1f4f1bf commit 7a7543f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/NetVips/GObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class GObject : SafeHandle
/// </summary>
private readonly ICollection<GCHandle> _handles = new List<GCHandle>();

/// <summary>
/// Hint of how much native memory is actually occupied by the object.
/// </summary>
internal long MemoryPressure;

// Handy for debugging
// public static int NObjects;

Expand Down
3 changes: 3 additions & 0 deletions src/NetVips/GValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public void Set(object value)
}
else if (fundamental == GObjectType && value is GObject gObject)
{
AddMemoryPressure(gObject.MemoryPressure);
Internal.GValue.SetObject(ref Struct, gObject);
}
else if (gtype == ArrayIntType)
Expand Down Expand Up @@ -351,6 +352,8 @@ public void Set(object value)

// the gvalue needs a ref on each of the images
image.ObjectRef();

AddMemoryPressure(image.MemoryPressure);
}
}
else if (gtype == BlobType && value is VipsBlob blob)
Expand Down
32 changes: 25 additions & 7 deletions src/NetVips/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ public static string FindLoadSource(Source source) =>
/// </remarks>
/// <param name="stream">The stream to test.</param>
/// <returns>The name of the load operation, or <see langword="null"/>.</returns>
public static string FindLoadStream(Stream stream) => FindLoadSource(SourceStream.NewFromStream(stream));
public static string FindLoadStream(Stream stream)
{
using (var source = SourceStream.NewFromStream(stream))
{
return FindLoadSource(source);
}
}

#endregion

Expand Down Expand Up @@ -491,8 +497,14 @@ public static Image NewFromStream(
string strOptions = "",
string access = null,
bool? fail = null,
VOption kwargs = null) =>
NewFromSource(SourceStream.NewFromStream(stream), strOptions, access, fail, kwargs);
VOption kwargs = null)
{
using (var source = SourceStream.NewFromStream(stream))
{
return NewFromSource(source, strOptions, access, fail, kwargs);
}
}


/// <summary>
/// Create an image from a 2D array.
Expand Down Expand Up @@ -707,7 +719,7 @@ public static Image NewFromMemoryCopy(
throw new VipsException("unable to make image from memory");
}

return new Image(vi);
return new Image(vi) { MemoryPressure = (long)size };
}

/// <summary>
Expand Down Expand Up @@ -815,7 +827,7 @@ public Image CopyMemory()
throw new VipsException("unable to copy to memory");
}

return new Image(vi);
return new Image(vi) { MemoryPressure = MemoryPressure };
}

#endregion
Expand Down Expand Up @@ -999,8 +1011,14 @@ public void WriteToTarget(Target target, string formatString, VOption kwargs = n
/// <param name="formatString">The suffix, plus any string-form arguments.</param>
/// <param name="kwargs">Optional options that depend on the save operation.</param>
/// <exception cref="VipsException">If unable to write to stream.</exception>
public void WriteToStream(Stream stream, string formatString, VOption kwargs = null) =>
WriteToTarget(TargetStream.NewFromStream(stream), formatString, kwargs);
public void WriteToStream(Stream stream, string formatString, VOption kwargs = null)
{
using (var target = TargetStream.NewFromStream(stream))
{
WriteToTarget(target, formatString, kwargs);
}
}


/// <summary>
/// Write the image to memory as a simple, unformatted C-style array.
Expand Down

0 comments on commit 7a7543f

Please sign in to comment.