//
// Copyright (c) 2002
// Ronald Kevin Burton
//
// Z poniższym kodem nie jest związana żadna gwarancja poprawności działania.
// Program został dołączony do książki ".NET CLR. Księga eksperta" w celu
// ilustracji koncepcji i zasad przedstawionych w tej książce. Program może być 
// używany na własne ryzyko.
//
// Przyznaje się prawo do użycia lub kopiowania tego oprogramowania do dowolnego celu
// bez konieczności ponoszenia żadnych opłat pod warunkiem, że powyższe uwagi zostaną 
// zachowane we wszystkich kopiach. Przyznaje się także prawo do modyfikacji kodu
// i dystrybucji zmodyfikowanego kodu pod warunkiem zachowania powyższych uwag
// oraz dołączenia informacji mówiącej o modyfikacji kodu.
//
// 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace LargeObject
{
	/// <summary>
	/// Podsumowanie dla Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		LargeObject lo;
		ArrayList objects;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox gen0Count;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.TextBox gen1Count;
		private System.Windows.Forms.TextBox gen2Count;
		private System.Windows.Forms.TextBox totalBytesAfter;
		private System.Windows.Forms.TextBox totalBytesBefore;
		private System.Windows.Forms.TextBox totalBytesDiff;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		/// <summary>
		/// Wymagana zmienna.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Wymagane do obsługi Windows Form Designer
			//
			InitializeComponent();

			objects = new ArrayList();

			//
			// TODO: Dodanie kodu konstruktora po wywołaniu InitializeComponent
			//
		}

		/// <summary>
		/// Oczyszczenie używanych zasobów.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Metoda wymagana do obsługi Designera - nie należy modyfikować
		/// zawartości tej metody przy użyciu edytora kodu.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.gen0Count = new System.Windows.Forms.TextBox();
			this.gen1Count = new System.Windows.Forms.TextBox();
			this.gen2Count = new System.Windows.Forms.TextBox();
			this.button3 = new System.Windows.Forms.Button();
			this.totalBytesAfter = new System.Windows.Forms.TextBox();
			this.totalBytesBefore = new System.Windows.Forms.TextBox();
			this.totalBytesDiff = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.Location = new System.Drawing.Point(8, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(256, 32);
			this.button1.TabIndex = 0;
			this.button1.Text = "Alokacja dużego obiektu";
			this.button1.Click += new System.EventHandler(this.OnAllocate);
			// 
			// button2
			// 
			this.button2.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.Location = new System.Drawing.Point(8, 56);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(256, 32);
			this.button2.TabIndex = 1;
			this.button2.Text = "Dealokacja dużego obiektu";
			this.button2.Click += new System.EventHandler(this.OnDeallocate);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 168);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 23);
			this.label1.TabIndex = 3;
			this.label1.Text = "Gen0";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(8, 200);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 23);
			this.label2.TabIndex = 4;
			this.label2.Text = "Gen1";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(8, 232);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 23);
			this.label3.TabIndex = 5;
			this.label3.Text = "Gen2";
			// 
			// gen0Count
			// 
			this.gen0Count.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.gen0Count.Location = new System.Drawing.Point(80, 160);
			this.gen0Count.Name = "gen0Count";
			this.gen0Count.ReadOnly = true;
			this.gen0Count.TabIndex = 6;
			this.gen0Count.Text = "";
			this.gen0Count.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// gen1Count
			// 
			this.gen1Count.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.gen1Count.Location = new System.Drawing.Point(80, 192);
			this.gen1Count.Name = "gen1Count";
			this.gen1Count.ReadOnly = true;
			this.gen1Count.TabIndex = 7;
			this.gen1Count.Text = "";
			this.gen1Count.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// gen2Count
			// 
			this.gen2Count.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.gen2Count.Location = new System.Drawing.Point(80, 224);
			this.gen2Count.Name = "gen2Count";
			this.gen2Count.ReadOnly = true;
			this.gen2Count.TabIndex = 8;
			this.gen2Count.Text = "";
			this.gen2Count.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// button3
			// 
			this.button3.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button3.Location = new System.Drawing.Point(8, 96);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(256, 32);
			this.button3.TabIndex = 9;
			this.button3.Text = "Collect";
			this.button3.Click += new System.EventHandler(this.OnCollect);
			// 
			// totalBytesAfter
			// 
			this.totalBytesAfter.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.totalBytesAfter.Location = new System.Drawing.Point(280, 192);
			this.totalBytesAfter.Name = "totalBytesAfter";
			this.totalBytesAfter.ReadOnly = true;
			this.totalBytesAfter.Size = new System.Drawing.Size(136, 31);
			this.totalBytesAfter.TabIndex = 12;
			this.totalBytesAfter.Text = "";
			this.totalBytesAfter.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// totalBytesBefore
			// 
			this.totalBytesBefore.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.totalBytesBefore.Location = new System.Drawing.Point(280, 160);
			this.totalBytesBefore.Name = "totalBytesBefore";
			this.totalBytesBefore.ReadOnly = true;
			this.totalBytesBefore.Size = new System.Drawing.Size(136, 31);
			this.totalBytesBefore.TabIndex = 13;
			this.totalBytesBefore.Text = "";
			this.totalBytesBefore.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// totalBytesDiff
			// 
			this.totalBytesDiff.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.totalBytesDiff.Location = new System.Drawing.Point(280, 224);
			this.totalBytesDiff.Name = "totalBytesDiff";
			this.totalBytesDiff.ReadOnly = true;
			this.totalBytesDiff.Size = new System.Drawing.Size(136, 31);
			this.totalBytesDiff.TabIndex = 14;
			this.totalBytesDiff.Text = "";
			this.totalBytesDiff.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(192, 168);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 23);
			this.label4.TabIndex = 15;
			this.label4.Text = "Przed";
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Location = new System.Drawing.Point(192, 200);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 23);
			this.label5.TabIndex = 16;
			this.label5.Text = "Po";
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Location = new System.Drawing.Point(192, 232);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 23);
			this.label6.TabIndex = 17;
			this.label6.Text = "Różnica";
			// 
			// label7
			// 
			this.label7.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label7.Location = new System.Drawing.Point(288, 136);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(120, 23);
			this.label7.TabIndex = 18;
			this.label7.Text = "Liczba bajtów";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(424, 266);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label7,
																		  this.label6,
																		  this.label5,
																		  this.label4,
																		  this.totalBytesDiff,
																		  this.totalBytesBefore,
																		  this.totalBytesAfter,
																		  this.button3,
																		  this.gen2Count,
																		  this.gen1Count,
																		  this.gen0Count,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.button2,
																		  this.button1});
			this.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "Form1";
			this.Text = "Formularz dużego obiektu";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Główny punkt wejścia dla aplikacji.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void OnAllocate(object sender, System.EventArgs e)
		{
			long tbb = GC.GetTotalMemory(false);
			lo = new LargeObject();
			long tba = GC.GetTotalMemory(false);

			if(lo != null)
			{
				int g = GC.GetGeneration(lo);
				int g0 = 0;
				int g1 = 0;
				int g2 = 0;
				if(g == 0)
					g0++;
				if(g == 1)
					g1++;
				if(g == 2)
					g2++;
				gen0Count.Text = Convert.ToString(g0);
				gen1Count.Text = Convert.ToString(g1);
				gen2Count.Text = Convert.ToString(g2);
			}
			totalBytesBefore.Text = Convert.ToString(tbb);
			totalBytesAfter.Text = Convert.ToString(tba);
			totalBytesDiff.Text = Convert.ToString(tba - tbb);
		}

		private void OnDeallocate(object sender, System.EventArgs e)
		{
			long tbb = GC.GetTotalMemory(false);
			lo = null;
			long tba = GC.GetTotalMemory(false);

			if(lo != null)
			{
				int g = GC.GetGeneration(lo);
				int g0 = 0;
				int g1 = 0;
				int g2 = 0;
				if(g == 0)
					g0++;
				if(g == 1)
					g1++;
				if(g == 2)
					g2++;
				gen0Count.Text = Convert.ToString(g0);
				gen1Count.Text = Convert.ToString(g1);
				gen2Count.Text = Convert.ToString(g2);
			}
			totalBytesBefore.Text = Convert.ToString(tbb);
			totalBytesAfter.Text = Convert.ToString(tba);
			totalBytesDiff.Text = Convert.ToString(tba - tbb);
		}

		private void OnCollect(object sender, System.EventArgs e)
		{
			long tbb = GC.GetTotalMemory(false);
			GC.Collect();
			long tba = GC.GetTotalMemory(false);

			if(lo != null)
			{
				int g = GC.GetGeneration(lo);
				int g0 = 0;
				int g1 = 0;
				int g2 = 0;
				if(g == 0)
					g0++;
				if(g == 1)
					g1++;
				if(g == 2)
					g2++;
				gen0Count.Text = Convert.ToString(g0);
				gen1Count.Text = Convert.ToString(g1);
				gen2Count.Text = Convert.ToString(g2);
			}

			totalBytesBefore.Text = Convert.ToString(tbb);
			totalBytesAfter.Text = Convert.ToString(tba);
			totalBytesDiff.Text = Convert.ToString(tba - tbb);
		}
	}
}
