Home All Groups Group Topic Archive Search About

Memory leak when SmtpMail and MailMessage are used.

Author
13 Jun 2006 6:02 PM
Pavel
I have the function which consists code below. Every 50 mails the memory of
application raises up 100k. Sometimes is more. Does someone know why and what
I have to do to solve this problem.

for(int emailNum = 0; emailNum < int.Parse(txtMailsAmount.Text); emailNum++)

{

MailMessage tmp = new MailMessage();

SmtpMail.SmtpServer = mail.SmtpServer;

tmp.BodyFormat = mail.BodyFormat;

tmp.Body = mail.Body;

tmp.Subject = mail.Subject;

tmp.From = mail.From;

tmp.To = mail.To;

tmp.Cc = mail.Cc;

SmtpMail.Send(tmp);


}

Author
13 Jun 2006 11:55 PM
Göran Andersson
It's normal. It's because the objects used aren't collected by the
garbage collector until the memory is needed.

Pavel wrote:
Show quote
> I have the function which consists code below. Every 50 mails the memory of
> application raises up 100k. Sometimes is more. Does someone know why and what
> I have to do to solve this problem.
>
> for(int emailNum = 0; emailNum < int.Parse(txtMailsAmount.Text); emailNum++)
>
> {
>
> MailMessage tmp = new MailMessage();
>
> SmtpMail.SmtpServer = mail.SmtpServer;
>
> tmp.BodyFormat = mail.BodyFormat;
>
> tmp.Body = mail.Body;
>
> tmp.Subject = mail.Subject;
>
> tmp.From = mail.From;
>
> tmp.To = mail.To;
>
> tmp.Cc = mail.Cc;
>
> SmtpMail.Send(tmp);
>
>
> }
Author
14 Jun 2006 2:54 AM
Michael D. Ober
You can assist the Garbage Collector by adding

tmp = Nothing

at the end of the loop.  However, you will still see some memory growth
until the GC runs.

Mike.

Show quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:ugQprT0jGHA.4264@TK2MSFTNGP02.phx.gbl...
> It's normal. It's because the objects used aren't collected by the
> garbage collector until the memory is needed.
>
> Pavel wrote:
> > I have the function which consists code below. Every 50 mails the memory
of
> > application raises up 100k. Sometimes is more. Does someone know why and
what
> > I have to do to solve this problem.
> >
> > for(int emailNum = 0; emailNum < int.Parse(txtMailsAmount.Text);
emailNum++)
> >
> > {
> >
> > MailMessage tmp = new MailMessage();
> >
> > SmtpMail.SmtpServer = mail.SmtpServer;
> >
> > tmp.BodyFormat = mail.BodyFormat;
> >
> > tmp.Body = mail.Body;
> >
> > tmp.Subject = mail.Subject;
> >
> > tmp.From = mail.From;
> >
> > tmp.To = mail.To;
> >
> > tmp.Cc = mail.Cc;
> >
> > SmtpMail.Send(tmp);
> >
> >
> > }
>
Author
14 Jun 2006 6:31 AM
Bruno Jouhier
"Michael D. Ober" <ober***@.alum.mit.edu.nospam> a écrit dans le message de
news: 6UKjg.5478$lf4.4***@newsread1.news.pas.earthlink.net...
> You can assist the Garbage Collector by adding
>
> tmp = Nothing
>
> at the end of the loop.  However, you will still see some memory growth
> until the GC runs.

No, this is useless. As tmp is reassigned at every iteration (tmp = new
MailMessage()), setting it to null (Nothing in VB) at the end of the loop
won't have any impact on the GC, it will only slow down your program.

On the side, calling int.Parse at every iteration to compute the upper bound
of the loop is a bad idea. You should rewrite the loop as:

int messageCount = int.Parse(txtMailsAmount.Text);
for (int i = 0; i < messageCount; i++)
    // loop body...

Show quote
>
> Mike.
>
> "Göran Andersson" <gu***@guffa.com> wrote in message
> news:ugQprT0jGHA.4264@TK2MSFTNGP02.phx.gbl...
>> It's normal. It's because the objects used aren't collected by the
>> garbage collector until the memory is needed.
>>
>> Pavel wrote:
>> > I have the function which consists code below. Every 50 mails the
>> > memory
> of
>> > application raises up 100k. Sometimes is more. Does someone know why
>> > and
> what
>> > I have to do to solve this problem.
>> >
>> > for(int emailNum = 0; emailNum < int.Parse(txtMailsAmount.Text);
> emailNum++)
>> >
>> > {
>> >
>> > MailMessage tmp = new MailMessage();
>> >
>> > SmtpMail.SmtpServer = mail.SmtpServer;
>> >
>> > tmp.BodyFormat = mail.BodyFormat;
>> >
>> > tmp.Body = mail.Body;
>> >
>> > tmp.Subject = mail.Subject;
>> >
>> > tmp.From = mail.From;
>> >
>> > tmp.To = mail.To;
>> >
>> > tmp.Cc = mail.Cc;
>> >
>> > SmtpMail.Send(tmp);
>> >
>> >
>> > }
>>
>
>
>
Author
16 Jun 2006 4:37 PM
Peter Ritchie
You should assign null to local reference variables when you know you're done
with it.  In a loop where the loop is re-assigning a new reference to the
variable setting it to null IS pointless.  At the end of the loop, if you
know you're done with tmp, assign null to it.

This allows the GC to collect the data sooner, if it can; rather than for
the variable to go out of scope.  It's best to make sure the scope of the
variable is such that it goes out of scope as soon as possible, making
assigning null to it moot.

Show quote
"Bruno Jouhier" wrote:

>
> "Michael D. Ober" <ober***@.alum.mit.edu.nospam> a écrit dans le message de
> news: 6UKjg.5478$lf4.4***@newsread1.news.pas.earthlink.net...
> > You can assist the Garbage Collector by adding
> >
> > tmp = Nothing
> >
> > at the end of the loop.  However, you will still see some memory growth
> > until the GC runs.
>
> No, this is useless. As tmp is reassigned at every iteration (tmp = new
> MailMessage()), setting it to null (Nothing in VB) at the end of the loop
> won't have any impact on the GC, it will only slow down your program.
>
> On the side, calling int.Parse at every iteration to compute the upper bound
> of the loop is a bad idea. You should rewrite the loop as:
>
> int messageCount = int.Parse(txtMailsAmount.Text);
> for (int i = 0; i < messageCount; i++)
>     // loop body...
>
> >
> > Mike.
> >
> > "Göran Andersson" <gu***@guffa.com> wrote in message
> > news:ugQprT0jGHA.4264@TK2MSFTNGP02.phx.gbl...
> >> It's normal. It's because the objects used aren't collected by the
> >> garbage collector until the memory is needed.
> >>
> >> Pavel wrote:
> >> > I have the function which consists code below. Every 50 mails the
> >> > memory
> > of
> >> > application raises up 100k. Sometimes is more. Does someone know why
> >> > and
> > what
> >> > I have to do to solve this problem.
> >> >
> >> > for(int emailNum = 0; emailNum < int.Parse(txtMailsAmount.Text);
> > emailNum++)
> >> >
> >> > {
> >> >
> >> > MailMessage tmp = new MailMessage();
> >> >
> >> > SmtpMail.SmtpServer = mail.SmtpServer;
> >> >
> >> > tmp.BodyFormat = mail.BodyFormat;
> >> >
> >> > tmp.Body = mail.Body;
> >> >
> >> > tmp.Subject = mail.Subject;
> >> >
> >> > tmp.From = mail.From;
> >> >
> >> > tmp.To = mail.To;
> >> >
> >> > tmp.Cc = mail.Cc;
> >> >
> >> > SmtpMail.Send(tmp);
> >> >
> >> >
> >> > }
> >>
> >
> >
> >
>
>
>

AddThis Social Bookmark Button