-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace GarbageCollection; | ||
|
||
public class Car : IDisposable | ||
{ | ||
private bool disposedValue; | ||
private string _brandName; | ||
|
||
public Car(string brandName) | ||
{ | ||
_brandName = brandName; | ||
} | ||
|
||
public string GetBrandName() | ||
{ | ||
return _brandName; | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
// TODO: dispose managed state (managed objects) | ||
} | ||
|
||
// TODO: free unmanaged resources (unmanaged objects) and override finalizer | ||
// TODO: set large fields to null | ||
disposedValue = true; | ||
} | ||
} | ||
|
||
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources | ||
// ~Car() | ||
// { | ||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
// Dispose(disposing: false); | ||
// } | ||
|
||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace GarbageCollection; | ||
|
||
public class Garbage | ||
{ | ||
public void CreateGarbage(int maxGarbage) | ||
{ | ||
Car car; | ||
for (int i = 0; i < maxGarbage; i++) | ||
{ | ||
car = new("Ferrari"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using GarbageCollection; | ||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
// using(Car car = new("Honda")) | ||
// { | ||
// // TODO.. | ||
// } | ||
|
||
// https://learn.microsoft.com/en-us/dotnet/api/system.gc?view=net-7.0 | ||
Garbage garbage = new(); | ||
Console.WriteLine("The highest generation is {0}", GC.MaxGeneration); | ||
Console.WriteLine("Generation: {0}", GC.GetGeneration(garbage)); | ||
garbage.CreateGarbage(1000); | ||
|
||
GC.Collect(0); | ||
Console.WriteLine("Generation: {0}", GC.GetGeneration(garbage)); | ||
|
||
GC.Collect(1); | ||
Console.WriteLine("Generation: {0}", GC.GetGeneration(garbage)); | ||
|
||
|
||
} | ||
} |