Skip to content

Instantly share code, notes, and snippets.

@FoC-
Created May 28, 2012 10:05
Show Gist options
  • Select an option

  • Save FoC-/2818295 to your computer and use it in GitHub Desktop.

Select an option

Save FoC-/2818295 to your computer and use it in GitHub Desktop.
Need to create class which could not been destroyed by GC
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Undead undead = null;
try
{
undead = new Undead(null);
}
catch { }
for (int i = 0; i < 15; i++)
{
Console.Write(new WeakReference(undead).IsAlive ? "Alive " : "Dead ");
Console.WriteLine(undead == null ? "No link" : "Link exist");
new HugeObject();
}
Console.ReadLine();
}
}
class Undead
{
private object o;
public string str = "Undead dead";
public Undead(object o)
{
this.o = o;
Console.WriteLine("I will die");
throw new NotImplementedException();
}
~Undead()
{
try
{
new Undead(this);
}
catch { }
if (o != null)
{
Console.WriteLine(((Undead) o).str);
str = "Kill me plz";
}
}
}
class HugeObject
{
string o = new string('0', 1024 * 1024 * 8);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment