ガベージコレクション テスト用アプリケーション
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WinForm1
{
///
/// Form1 の概要の説明です。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.RadioButton rbNone;
private System.Windows.Forms.RadioButton rbDispose;
private System.Windows.Forms.RadioButton rbUsing;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.CheckBox checkBoxUseImage;
private System.Windows.Forms.Label labelCount;
///
/// 必要なデザイナ変数です。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、
// コンストラクタ コードを追加してください。
//
}
///
/// 使用されているリソースに後処理を実行します。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
///
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.label3 = new System.Windows.Forms.Label();
this.rbNone = new System.Windows.Forms.RadioButton();
this.rbDispose = new System.Windows.Forms.RadioButton();
this.rbUsing = new System.Windows.Forms.RadioButton();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.checkBoxUseImage = new System.Windows.Forms.CheckBox();
this.labelCount = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(296, 128);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Run";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Font = new System.Drawing.Font("MS UI Gothic",
18F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
this.label1.Location = new System.Drawing.Point(176, 168);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 32);
this.label1.TabIndex = 2;
this.label1.Text = "0.0";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// button3
//
this.button3.Location = new System.Drawing.Point(40, 208);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 24);
this.button3.TabIndex = 3;
this.button3.Text = "Do GC 0";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(120, 208);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 24);
this.button4.TabIndex = 4;
this.button4.Text = "Do GC 1";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(208, 208);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(64, 24);
this.button5.TabIndex = 5;
this.button5.Text = "Do GC 2";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(304, 208);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(64, 24);
this.button6.TabIndex = 6;
this.button6.Text = "Do GC All";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// label2
//
this.label2.Font = new System.Drawing.Font("MS UI Gothic",
18F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
this.label2.Location = new System.Drawing.Point(304, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 24);
this.label2.TabIndex = 7;
this.label2.Text = "SEC";
//
// numericUpDown1
//
this.numericUpDown1.Font = new System.Drawing.Font("MS UI Gothic",
15.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
this.numericUpDown1.Location = new System.Drawing.Point(288, 16);
this.numericUpDown1.Maximum = new System.Decimal(new int[] {
1000000,
0,
0,
0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(88, 28);
this.numericUpDown1.TabIndex = 8;
this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.numericUpDown1.Value = new System.Decimal(new int[] {
500,0,0,0});
//
// label3
//
this.label3.Font = new System.Drawing.Font("MS UI Gothic",
15.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
this.label3.Location = new System.Drawing.Point(216, 16);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 24);
this.label3.TabIndex = 9;
this.label3.Text = "Repeat";
//
// rbNone
//
this.rbNone.Checked = true;
this.rbNone.Location = new System.Drawing.Point(16, 24);
this.rbNone.Name = "rbNone";
this.rbNone.Size = new System.Drawing.Size(80, 32);
this.rbNone.TabIndex = 10;
this.rbNone.TabStop = true;
this.rbNone.Text = "Auto GC";
//
// rbDispose
//
this.rbDispose.Location = new System.Drawing.Point(16, 48);
this.rbDispose.Name = "rbDispose";
this.rbDispose.Size = new System.Drawing.Size(80, 32);
this.rbDispose.TabIndex = 11;
this.rbDispose.Text = "Dispose";
//
// rbUsing
//
this.rbUsing.Location = new System.Drawing.Point(16, 72);
this.rbUsing.Name = "rbUsing";
this.rbUsing.Size = new System.Drawing.Size(80, 32);
this.rbUsing.TabIndex = 12;
this.rbUsing.Text = "Using";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.rbNone);
this.groupBox1.Controls.Add(this.rbDispose);
this.groupBox1.Controls.Add(this.rbUsing);
this.groupBox1.Location = new System.Drawing.Point(176, 48);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(104, 112);
this.groupBox1.TabIndex = 13;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Memory";
//
// button2
//
this.button2.Location = new System.Drawing.Point(296, 88);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 14;
this.button2.Text = "Set Image";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// checkBoxUseImage
//
this.checkBoxUseImage.Location = new System.Drawing.Point(296, 64);
this.checkBoxUseImage.Name = "checkBoxUseImage";
this.checkBoxUseImage.Size = new System.Drawing.Size(80, 16);
this.checkBoxUseImage.TabIndex = 15;
this.checkBoxUseImage.Text = "Use Image";
//
// labelCount
//
this.labelCount.Font = new System.Drawing.Font("MS UI Gothic",
18F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
this.labelCount.Location = new System.Drawing.Point(32, 168);
this.labelCount.Name = "labelCount";
this.labelCount.Size = new System.Drawing.Size(112, 32);
this.labelCount.TabIndex = 16;
this.labelCount.Text = "0.0";
this.labelCount.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(392, 246);
this.Controls.Add(this.labelCount);
this.Controls.Add(this.checkBoxUseImage);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.label2);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Garbage Collection Test";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// アプリケーションのメイン エントリ ポイントです。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
DateTime start = DateTime.Now;
for(int i = 0; i < this.numericUpDown1.Value; i++)
{
this.Refresh();
this.labelCount.Text = i.ToString();
}
DateTime end = DateTime.Now;
TimeSpan diff = end - start;
this.label1.Text = diff.TotalSeconds.ToString("f3");
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (this.rbNone.Checked)
{
Graphics g = e.Graphics;
if (this.checkBoxUseImage.Checked)
{
Bitmap bmp = new Bitmap(fname);
g.DrawImage(bmp, 0,0, 100, 100);
}
Pen rPen = new Pen(Color.Red, 1.0F);
g.DrawLine(rPen, 0, 0, 100, 100);
SolidBrush brush = new SolidBrush(SystemColors.WindowText);
Font font = new Font("Courier", 12);
g.DrawString("文字列", font, brush, 50, 50);
}
else if (this.rbDispose.Checked)
{
Graphics g = e.Graphics;
if (this.checkBoxUseImage.Checked)
{
Bitmap bmp = new Bitmap(fname);
g.DrawImage(bmp, 0,0, 100, 100);
bmp.Dispose();
}
Pen rPen = new Pen(Color.Red, 1.0F);
g.DrawLine(rPen, 0, 0, 100, 100);
rPen.Dispose();
SolidBrush brush = new SolidBrush(SystemColors.WindowText);
Font font = new Font("Courier", 12);
g.DrawString("文字列", font, brush, 50, 50);
font.Dispose();
g.Dispose();
}
else if (this.rbUsing.Checked)
{
using (Graphics g = e.Graphics)
{
if (this.checkBoxUseImage.Checked)
{
using (Bitmap bmp = new Bitmap(fname))
g.DrawImage(bmp, 0,0, 100, 100);
}
using (SolidBrush brush =
new SolidBrush(SystemColors.WindowText))
using (Font font = new Font("Courier", 12))
using (Pen rPen = new Pen(Color.Red, 1.0F))
{
g.DrawLine(rPen, 0, 0, 100, 100);
g.DrawString("文字列", font, brush, 50, 50);
}
}
}
}
private void button3_Click(object sender, System.EventArgs e)
{
GC.Collect(0);
}
private void button4_Click(object sender, System.EventArgs e)
{
GC.Collect(1);
}
private void button5_Click(object sender, System.EventArgs e)
{
GC.Collect(2);
}
private void button6_Click(object sender, System.EventArgs e)
{
GC.Collect();
}
private string fname = null;
private void button2_Click(object sender, System.EventArgs e)
{
DialogResult res = this.openFileDialog1.ShowDialog();
if (res == DialogResult.OK)
{
fname = this.openFileDialog1.FileName;
this.checkBoxUseImage.Checked = true;
Invalidate();
}
}
}
}
|