HELP! How to free memory used by Form object

M

Maciek Bojczuk

Hi,
Please tell me how I can free the memory used by Form object. I create a
new form object something like this:

Form setup = new settings();
setup.ShowDialog();

Please help me in this problem. I traying used the setup.dispose(true);
but it doesn't work.

Maciek
 
M

Marc Gravell

The caller should really be calling Dispose(), not Dispose(bool) - the
easiest way to do this is via "using":

using(Form setup = new settings()) {
setup.ShowDialog();
// perhaps read some values...
}

This should help with most things, but there may be some things you
have added to the form yourself that aren't cleaned up; hard to say
without more info.

Note that GC and finalization isn't deterministic, but with the above
your form should evaporate fairly soon. Is there something specific
that makes you suspect that it is hanging around? If you let us know,
it might give more context...

Another *big* thing to note is that if your form has subscribed to
external events (perhaps static events), then those subscriptions will
prevent the form from being collected. You don't need to worry about
events that are internal to the form (i.e. handling a TextChanged on a
TextBox or whatever), but if you subscribe to something that will live
for ages, then you should unsubscribe as part of cleanup.

Marc
 
M

Maciek Bojczuk

I try your solution, but it doesn't work:

private void toolStripButton1_Click(object sender, EventArgs e)
{ //nawiązanie połączenia
using (Form Connect = new Connect(ref SP))
{
Connect.ShowDialog();
}
}
This is a Connect form (created in visual edit mode):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace sht15
{
public partial class Connect : Form
{
public Connect(ref SerialPort Serial)
{
InitializeComponent();
search_ports(ref Serial);

}
public void search_ports(ref SerialPort SP)
{
for (int i = 1; i <= 32; i++)
{
try
{
SP.PortName = "COM" + i.ToString();
SP.Open();
SP.Close();
port_name.Items.Add("Com " + i.ToString());
port_name.SelectedIndex = 0;
}
catch { }
}
log.Items.Add("Wyszukiwanie portów zakończone");
}

private void OK_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
}



Marc Gravell pisze:
 
M

Marc Gravell

OK; first off, your OK button should probably be calling Close() -
however; can you please define "doesn't work"? What makes you say
that? What behavior / error / whatever are you seeing?
 
M

Maciek Bojczuk

I checking a memory usage in task manager. When I open a connect Form,
the memory usage is increment, but when I close the connect form (Click
OK button, Calling: this.Close()) momory usage don't decrement, stay in
this same value when the form was open.

Marc Gravell pisze:
 
M

Marc Gravell

I checking a memory usage in task manager.

Ah. Problem sorted. Task manager reports private bytes (pre-vista, at
least) - which is *not* the amount of memory currently in use. Freaky,
but true. Try minimising, for example.

http://www.itwriting.com/dotnetmem.php
http://blogs.msdn.com/tess/archive/2006/09/06/net-memory-usage-a-restaurant-analogy.aspx
etc

Essentially, this counter is useless except as a very broad brush. You
might want to use some slightly more specific tests for memory
allocation. Perhaps specific .NET memory profilers, but IIRC, the free
sysinternals process explorer displays some quite useful .NET metrics;
can't remember specifics...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top