Click here to Skip to main content
15,888,579 members
Home / Discussions / C#
   

C#

 
QuestionHow to check object==null for objects without Constructor? Pin
artisticcheese4-Nov-05 6:05
artisticcheese4-Nov-05 6:05 
AnswerRe: How to check object==null for objects without Constructor? Pin
whizzs4-Nov-05 7:27
whizzs4-Nov-05 7:27 
QuestionLoading Files Pin
zaboboa4-Nov-05 5:53
zaboboa4-Nov-05 5:53 
AnswerRe: Loading Files Pin
User 66584-Nov-05 7:32
User 66584-Nov-05 7:32 
GeneralRe: Loading Files Pin
zaboboa4-Nov-05 8:13
zaboboa4-Nov-05 8:13 
AnswerRe: Loading Files Pin
whizzs4-Nov-05 7:37
whizzs4-Nov-05 7:37 
AnswerRe: Loading Files Pin
Jon Rista4-Nov-05 16:16
Jon Rista4-Nov-05 16:16 
Questionget GC to collect unused memory Pin
Stefan Fohringer4-Nov-05 5:06
Stefan Fohringer4-Nov-05 5:06 
hi there!
i wrote a little application to demonstrate a little issue of mine.
just compile this source and check the memory used by the application (taskman or process-explorer)

the application starts with about 6mb memory.
when the last messagebox is displayed, everything should be disposed and the memory should return nearly to 6mb. but it doesn't.

has anyone any idea how to get the GC to collect this unused (but reserved?) memory?
i have to engineer a little application which will run on a terminalserver for each user. (50users x 10mb unneccessary memory is not very good...)

best regards

#code begin
using System;
using System.Windows.Forms;

namespace GCTest
{
///
/// Zusammenfassung für Class1.
///

class Class1 : Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;

private bool running = true;
private System.Collections.Hashtable mem;
///
/// Der Haupteinstiegspunkt für die Anwendung.
///

[STAThread]
static void Main(string[] args)
{
Class1 cl = new Class1();
Application.Run(cl);

MessageBox.Show(GC.GetGeneration(cl) + "","wait");
GC.Collect(2);
MessageBox.Show("wait","wait");
}

public Class1()
{
InitializeComponent();

System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Runner));
t.Start();
}

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "alloc";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(40, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(152, 23);
this.button2.TabIndex = 0;
this.button2.Text = "set objects null";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(40, 120);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(152, 23);
this.button3.TabIndex = 0;
this.button3.Text = "set hashtable null";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(40, 160);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(152, 23);
this.button4.TabIndex = 0;
this.button4.Text = "exit";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Class1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button4);
this.Name = "Class1";
this.Text = "Bla";
this.ResumeLayout(false);

}


private void Runner()
{
while (running)
{
GC.Collect();
GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(5000);
}
}

private void button1_Click(object sender, System.EventArgs e)
{
mem = new System.Collections.Hashtable();

for (int i = 0; i < 1000000; i++)
mem.Add(i,new Consumer());
}

private void button2_Click(object sender, System.EventArgs e)
{
for (int i = 0; i < mem.Count; i++)
mem[i] = null;
}

private void button3_Click(object sender, System.EventArgs e)
{
mem.Clear();
mem = null;
}

private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
running = false;
}
}

public class Consumer
{
private string _str;

public Consumer()
{
_str = "abcdefghijklmnopqrst";
}
}
}
#code end
AnswerRe: get GC to collect unused memory Pin
Dan Neely4-Nov-05 5:32
Dan Neely4-Nov-05 5:32 
AnswerRe: get GC to collect unused memory Pin
Dave Kreskowiak4-Nov-05 5:52
mveDave Kreskowiak4-Nov-05 5:52 
GeneralRe: get GC to collect unused memory Pin
Jon Rista4-Nov-05 16:33
Jon Rista4-Nov-05 16:33 
GeneralRe: get GC to collect unused memory Pin
Dave Kreskowiak5-Nov-05 2:49
mveDave Kreskowiak5-Nov-05 2:49 
GeneralRe: get GC to collect unused memory Pin
Jon Rista5-Nov-05 9:57
Jon Rista5-Nov-05 9:57 
QuestionMost elegant way to query 400 computers in C#? Pin
artisticcheese4-Nov-05 4:44
artisticcheese4-Nov-05 4:44 
AnswerRe: Most elegant way to query 400 computers in C#? Pin
Rob Graham4-Nov-05 5:14
Rob Graham4-Nov-05 5:14 
GeneralRe: Most elegant way to query 400 computers in C#? Pin
artisticcheese4-Nov-05 5:17
artisticcheese4-Nov-05 5:17 
AnswerRe: Most elegant way to query 400 computers in C#? Pin
S. Senthil Kumar4-Nov-05 21:39
S. Senthil Kumar4-Nov-05 21:39 
GeneralRe: Most elegant way to query 400 computers in C#? Pin
artisticcheese5-Nov-05 2:31
artisticcheese5-Nov-05 2:31 
GeneralRe: Most elegant way to query 400 computers in C#? Pin
S. Senthil Kumar5-Nov-05 3:17
S. Senthil Kumar5-Nov-05 3:17 
QuestionRemoting - recovering communication Pin
duff794-Nov-05 4:37
duff794-Nov-05 4:37 
QuestionAny good C# 2.0 books out yet? Pin
Ed K4-Nov-05 3:46
Ed K4-Nov-05 3:46 
QuestionUnTrapping of Mouse Events Pin
wheelerbarry4-Nov-05 3:38
wheelerbarry4-Nov-05 3:38 
AnswerRe: UnTrapping of Mouse Events Pin
Dave Kreskowiak4-Nov-05 5:47
mveDave Kreskowiak4-Nov-05 5:47 
QuestionRichtextbox Pin
PaulaM4-Nov-05 2:38
PaulaM4-Nov-05 2:38 
AnswerRe: Richtextbox Pin
mav.northwind4-Nov-05 2:49
mav.northwind4-Nov-05 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.