Reputation: 307
I want to completely optimize a class so that it never produces garbage during its life cycle (excluding instantiation and destruction) but I also want to return an object from a method - is this possible?
Here is an example of such a situation:
public class Example
{
/// <summary>
/// Width of each column - gets set by the constructor.
/// </summary>
private int columnWidth = 0;
/// <summary>
/// Height of each row - gets set by the constructor.
/// </summary>
private int rowHeight = 0;
/// <summary>
/// Internal working rectangle to avoid garbage collection.
/// Created only once for each instance of encapsulating class.
/// </summary>
private Rectangle rectangle = new Rectangle(0, 0, 0, 0);
/// <summary>
/// Initializes a new instance of the <see ref="Example"> class.
/// </summary>
/// <param name="columnWidth">Column width.</param>
/// <param name="rowHeight">Row height.</param>
public Example(int columnWidth, int rowHeight)
{
this.columnWidth = columnWidth;
this.rowHeight = rowHeight;
}
/// <summary>
/// Constructs and returns a rectangle for this class.
/// </summary>
/// <param name="column">Column for the rectangle to represent.</param>
/// <param name="row">Row for the rectangle to represent.</param>
/// <returns>Constructed rectangle for column and row.</returns>
public Rectangle GetRectangle(int column, int row)
{
this.rectangle.Width = this.columnWidth;
this.rectangle.Height = this.rowHeight;
this.rectangle.X = column * this.columnWidth;
this.rectangle.Y = row * this.rowHeight;
return this.rectangle;
}
}
public class OtherClass
{
// some other unimportant stuff going on...
/// <summary>
/// Internal rectangle.
/// </summary>
private Rectangle rec = new Rectangle(0, 0, 0, 0);
/// <summary>
/// Does some stuff.
/// </summary>
private void DoStuff()
{
var exampleClass = new Example(20, 20);
// --- THIS IS THE PART I AM CONCERNED ABOUT ---
// Does this assignment over-write the previous assignment (no garbage)?
// - or -
// Does this produce a new assignment and orphan the old one (creates garbage)?
this.rec = exampleClass.GetRectangle(1, 3);
}
// some more other unimportant stuff going on...
}
I guess it would be more accurate to ask whether assigning an object to a reference that already exists produces garbage.
-- [Edit] --
Just of context, if you're wondering: this method will be used to get the source rectangle for a sprite from a sprite sheet. Since the sprite sheets have animation strips, this call will be made countless times per second so any orphaned objects will add up pretty quickly.
Upvotes: 1
Views: 324
Reputation: 1791
If the object being assigned to is a value type, then the value would be cleanly replaced. Otherwise, if its a reference type, it would orphan the previous object and mark it “unreachable”, which could later be removed/collected by the GarbageCollector.
In our scenario, I believe, its a value type (struct), hence it would be replaced without producing garbage.
Furthermore, the GarbageCollector has no fixed interval to run and collect, it runs based on the conditions mentioned in the following links:
You’ll also find these useful:
C# The 'new' keyword on existing objects
https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/
Upvotes: 1