Created
December 11, 2012 23:57
-
-
Save anonymous/4263527 to your computer and use it in GitHub Desktop.
checking if fixed statement influences GC generations
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
using System; | |
namespace ConsoleApplication1 | |
{ | |
public class Point | |
{ | |
public int x; | |
public int y; | |
} | |
internal class Program | |
{ | |
private static unsafe void Main(string[] args) | |
{ | |
var point = new Point(); | |
fixed (void* p = &point.x) | |
{ | |
Console.WriteLine("inside fixed: object is in Gen{0}", GC.GetGeneration(point)); | |
GC.Collect(); | |
Console.WriteLine("inside fixed: after GC object is in Gen{0}", GC.GetGeneration(point)); | |
} | |
Console.WriteLine("outside fixed: object is in Gen{0}", GC.GetGeneration(point)); | |
GC.Collect(); | |
Console.WriteLine("outside fixed: after GC object is in Gen{0}", GC.GetGeneration(point)); | |
// Output: | |
// | |
//inside fixed: object is in Gen0 | |
//inside fixed: after GC object is in Gen1 | |
//outside fixed: object is in Gen1 | |
//outside fixed: after GC object is in Gen2 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment