// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Diagnostics; using System.Drawing; using System.Windows.Forms; namespace E { public class MainForm : Form { byte[] b1; byte[] b3; byte[] b9; byte[] b27; private Label dotnet2app; private Label win2dotnet; private Button alloc1; private Button free1; private Button alloc3; private Button free3; private Button alloc9; private Button free9; private Button alloc27; private Button free27; private Button rungc; public MainForm() { InitializeComponent(); } private void InitializeComponent() { dotnet2app = new Label(); win2dotnet = new Label(); alloc1 = new Button(); free1 = new Button(); alloc3 = new Button(); free3 = new Button(); alloc9 = new Button(); free9 = new Button(); alloc27 = new Button(); free27 = new Button(); rungc = new Button(); SuspendLayout(); dotnet2app.Location = new Point(50, 50); dotnet2app.Size = new Size(200, 50); win2dotnet.Location = new Point(300, 50); win2dotnet.Size = new Size(200, 50); alloc1.Location = new Point(50, 150); alloc1.Size = new Size(100, 50); alloc1.Text = "Allocate 1 MB"; alloc1.Click += new EventHandler(Alloc1Click); free1.Location = new Point(300, 150); free1.Size = new Size(100, 50); free1.Text = "Free 1 MB"; free1.Click += new EventHandler(Free1Click); alloc3.Location = new Point(50, 250); alloc3.Size = new Size(100, 50); alloc3.Text = "Allocate 3 MB"; alloc3.Click += new EventHandler(Alloc3Click); free3.Location = new Point(300, 250); free3.Size = new Size(100, 50); free3.Text = "Free 3 MB"; free3.Click += new EventHandler(Free3Click); alloc9.Location = new Point(50, 350); alloc9.Size = new Size(100, 50); alloc9.Text = "Allocate 9 MB"; alloc9.Click += new EventHandler(Alloc9Click); free9.Location = new Point(300, 350); free9.Size = new Size(100, 50); free9.Text = "Free 9 MB"; free9.Click += new EventHandler(Free9Click); alloc27.Location = new Point(50, 450); alloc27.Size = new Size(100, 50); alloc27.Text = "Allocate 27 MB"; alloc27.Click += new EventHandler(Alloc27Click); free27.Location = new Point(300, 450); free27.Size = new Size(100, 50); free27.Text = "Free 27 MB"; free27.Click += new EventHandler(Free27Click); rungc.Location = new Point(175, 550); rungc.Size = new Size(100, 50); rungc.Text = "Run garbage collection"; rungc.Click += new EventHandler(RungcClick); ClientSize = new Size(550, 650); Controls.Add(dotnet2app); Controls.Add(win2dotnet); Controls.Add(alloc1); Controls.Add(free1); Controls.Add(alloc3); Controls.Add(free3); Controls.Add(alloc9); Controls.Add(free9); Controls.Add(alloc27); Controls.Add(free27); Controls.Add(rungc); Text = "Memory allocation demo"; UpdateStatus(); ResumeLayout(false); } private void UpdateStatus() { dotnet2app.Text = "Application has allocated from .NET " + GC.GetTotalMemory(false)/1000000 + " MB"; win2dotnet.Text = ".NET has allocated from Windows " + Process.GetCurrentProcess().PrivateMemorySize/1000000 + " MB"; } private void Alloc1Click(object sender, EventArgs e) { b1 = new byte[1000000]; UpdateStatus(); } private void Free1Click(object sender, EventArgs e) { b1 = null; UpdateStatus(); } private void Alloc3Click(object sender, EventArgs e) { b3 = new byte[3000000]; UpdateStatus(); } private void Free3Click(object sender, EventArgs e) { b3 = null; UpdateStatus(); } private void Alloc9Click(object sender, EventArgs e) { b9 = new byte[9000000]; UpdateStatus(); } private void Free9Click(object sender, EventArgs e) { b9 = null; UpdateStatus(); } private void Alloc27Click(object sender, EventArgs e) { b27 = new byte[27000000]; UpdateStatus(); } private void Free27Click(object sender, EventArgs e) { b27 = null; UpdateStatus(); } private void RungcClick(object sender, EventArgs e) { GC.Collect(); UpdateStatus(); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } } }