By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Disposing Objects?

P: n/a
Hi,

I am developing a financial application and you know that there are huge
amount of data in these similar aplications. I have a MDIChild form and the
other forms are opened in this form. For example, in a form i fetch many
rows from the database to show them to the user on the client machine. After
this process memory gets higher but when the user closes that form the
memory remains same amount.

I am using IDisposable interface for my entities and i use GC.Collect() and
GC.SuppressFinalize() method for my entities while they are being disposed.
But i cannot achieve to reduce the amount of the memory. How can i reduce
the amount of memory after the user closes the form? Is there a way to get
that result immediate after the user closes the form in C#? Thanks...
Oct 17 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
How are you measuring the memory footprint?

Mike Ober.

"Adam Right" <ad**@right.comwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
Hi,

I am developing a financial application and you know that there are huge
amount of data in these similar aplications. I have a MDIChild form and
the other forms are opened in this form. For example, in a form i fetch
many rows from the database to show them to the user on the client
machine. After this process memory gets higher but when the user closes
that form the memory remains same amount.

I am using IDisposable interface for my entities and i use GC.Collect()
and GC.SuppressFinalize() method for my entities while they are being
disposed. But i cannot achieve to reduce the amount of the memory. How can
i reduce the amount of memory after the user closes the form? Is there a
way to get that result immediate after the user closes the form in C#?
Thanks...


Oct 17 '06 #2

P: n/a
Hi Mike,

I do not understand what do you mean with Memory footprint? If you ask where
am i looking for the current memory size, i use task manager. Can you
explain in details? Thanks...

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
How are you measuring the memory footprint?

Mike Ober.

"Adam Right" <ad**@right.comwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
>Hi,

I am developing a financial application and you know that there are huge
amount of data in these similar aplications. I have a MDIChild form and
the other forms are opened in this form. For example, in a form i fetch
many rows from the database to show them to the user on the client
machine. After this process memory gets higher but when the user closes
that form the memory remains same amount.

I am using IDisposable interface for my entities and i use GC.Collect()
and GC.SuppressFinalize() method for my entities while they are being
disposed. But i cannot achieve to reduce the amount of the memory. How
can i reduce the amount of memory after the user closes the form? Is
there a way to get that result immediate after the user closes the form
in C#? Thanks...



Oct 17 '06 #3

P: n/a


On Oct 17, 10:13 am, "Adam Right" <a...@right.comwrote:
Hi,

I am developing a financial application and you know that there are huge
amount of data in these similar aplications. I have a MDIChild form and the
other forms are opened in this form. For example, in a form i fetch many
rows from the database to show them to the user on the client machine. After
this process memory gets higher but when the user closes that form the
memory remains same amount.

I am using IDisposable interface for my entities and i use GC.Collect() and
GC.SuppressFinalize() method for my entities while they are being disposed.
But i cannot achieve to reduce the amount of the memory. How can i reduce
the amount of memory after the user closes the form? Is there a way to get
that result immediate after the user closes the form in C#? Thanks...
Adam,

First, I just want to clarify the situations where IDisposable should
be used. Use IDisposable only for class that directly or indirectly
hold unmanaged resources. If your objects just hold information about
entities from the database then I'd bet they don't hold unmanaged
resources at all. Second, you should only be overriding the finalizer
(destructor) and calling GC.SuppressFinalize when the class directly
holds unmanaged resources. Third, GC.Collect is rarely needed. The
runtime does a pretty good job determining when the GC needs to run.
Manual calls to GC.Collect often lead to poorer performing
applications.

Are you certain these objects are no longer referenced anywhere?

Brian

Oct 17 '06 #4

P: n/a
Hi Brian,

Firstly i want to clarify the meaning of unmanaged and managed code. I did
not understand at all so i need help about that. Second, my baseentity has
some fields which are another entity or arraylist or hashtable. So they are
all referenced types. According to this i dispose them when the dispose
method is called but i donot dispose them when the destructor of the entity
is called. Calling GC.SuppressFinalize(entity) is done in the same manner.

On the other hand, i only call the GC.Collect() when i closes the form. I
use it in my form base. I know that GC.Collect() is not good for the
performance. But the problem is the client memory and i have to reduce the
amount of the memory after the user closes the form. In brief, all objects
that were created for the related form and its controls, must be released
after the form is closed.

For your question, how can i find the answer of "the objects are no longer
referenced anywhere?"? In C#, couldn't we release the all objects after the
related form is closed? I think we should because it is C#. Thanks...
"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>

On Oct 17, 10:13 am, "Adam Right" <a...@right.comwrote:
>Hi,

I am developing a financial application and you know that there are huge
amount of data in these similar aplications. I have a MDIChild form and
the
other forms are opened in this form. For example, in a form i fetch many
rows from the database to show them to the user on the client machine.
After
this process memory gets higher but when the user closes that form the
memory remains same amount.

I am using IDisposable interface for my entities and i use GC.Collect()
and
GC.SuppressFinalize() method for my entities while they are being
disposed.
But i cannot achieve to reduce the amount of the memory. How can i reduce
the amount of memory after the user closes the form? Is there a way to
get
that result immediate after the user closes the form in C#? Thanks...

Adam,

First, I just want to clarify the situations where IDisposable should
be used. Use IDisposable only for class that directly or indirectly
hold unmanaged resources. If your objects just hold information about
entities from the database then I'd bet they don't hold unmanaged
resources at all. Second, you should only be overriding the finalizer
(destructor) and calling GC.SuppressFinalize when the class directly
holds unmanaged resources. Third, GC.Collect is rarely needed. The
runtime does a pretty good job determining when the GC needs to run.
Manual calls to GC.Collect often lead to poorer performing
applications.

Are you certain these objects are no longer referenced anywhere?

Brian

Oct 17 '06 #5

P: n/a


"Adam Right" <ad**@right.comwrote in message
news:#S**************@TK2MSFTNGP04.phx.gbl...
Hi Brian,

Firstly i want to clarify the meaning of unmanaged and managed code. I did
not understand at all so i need help about that. Second, my baseentity has
some fields which are another entity or arraylist or hashtable. So they
are all referenced types. According to this i dispose them when the
dispose method is called but i donot dispose them when the destructor of
the entity is called. Calling GC.SuppressFinalize(entity) is done in the
same manner.

On the other hand, i only call the GC.Collect() when i closes the form. I
use it in my form base. I know that GC.Collect() is not good for the
performance. But the problem is the client memory and i have to reduce the
amount of the memory after the user closes the form. In brief, all objects
that were created for the related form and its controls, must be released
after the form is closed.
. .
If you are going to call GC.Collect() and expect the form and all of it's
reachable objects to be collected, you need to do it _after_ the form is
closed and and no reference to the form exists from any of your application
roots.

So calling GC.Collect() from the form itself is the wrong time. You should
do it from the bit of code that created the form, after the form has been
closed and any references to the form cleared.

David
Oct 17 '06 #6

P: n/a
If you are going to call GC.Collect() and expect the form and all of
it's reachable objects to be collected, you need to do it _after_ the
form is closed and and no reference to the form exists from any of
your application roots.

So calling GC.Collect() from the form itself is the wrong time. You
should do it from the bit of code that created the form, after the
form has been closed and any references to the form cleared.
In addition, because a Form ultimately overrides the Object.Finalize() method
in the System.CompontentModel.Component ancestor, it will take two garbage
collections to free its memory. This is because, an object with a finalizer
is placed in a separate internal table (the "freachable" table) and survies
the first GC. See the "Finalization Internals" section in Jeffrey Richter's
artcile for details: http://msdn.microsoft.com/msdnmag/issues/1100/gci/

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 17 '06 #7

P: n/a


"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>If you are going to call GC.Collect() and expect the form and all of
it's reachable objects to be collected, you need to do it _after_ the
form is closed and and no reference to the form exists from any of
your application roots.

So calling GC.Collect() from the form itself is the wrong time. You
should do it from the bit of code that created the form, after the
form has been closed and any references to the form cleared.

In addition, because a Form ultimately overrides the Object.Finalize()
method in the System.CompontentModel.Component ancestor, it will take two
garbage collections to free its memory. This is because, an object with a
finalizer is placed in a separate internal table (the "freachable" table)
and survies the first GC. See the "Finalization Internals" section in
Jeffrey Richter's artcile for details:
http://msdn.microsoft.com/msdnmag/issues/1100/gci/
Form.Close() calls Component.Dispose() which calls GC.SupressFinalize(this).
So properly closed forms will not be on the FReachable queue and will be
collected with one pass.

David

Oct 17 '06 #8

P: n/a
Form.Close() calls Component.Dispose() which calls
GC.SupressFinalize(this). So properly closed forms will not be on the
FReachable queue and will be collected with one pass.
Of course you're right. My apologies for jumping in their without thinking
it through.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 17 '06 #9

P: n/a

Hi,

To achieve reducing the amount of memory to the initial value which is
before opening the form, is there a way to get this expected result after
closing the form in C#? I have tried IDisposable interface for my entities
and GC.Collect() but it did not work.

Thanks...

"Adam Right" <ad**@right.comwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
Hi,

I am developing a financial application and you know that there are huge
amount of data in these similar aplications. I have a MDIChild form and
the other forms are opened in this form. For example, in a form i fetch
many rows from the database to show them to the user on the client
machine. After this process memory gets higher but when the user closes
that form the memory remains same amount.

I am using IDisposable interface for my entities and i use GC.Collect()
and GC.SuppressFinalize() method for my entities while they are being
disposed. But i cannot achieve to reduce the amount of the memory. How can
i reduce the amount of memory after the user closes the form? Is there a
way to get that result immediate after the user closes the form in C#?
Thanks...


Oct 18 '06 #10

P: n/a
To achieve reducing the amount of memory to the initial value which is
before opening the form, is there a way to get this expected result
after closing the form in C#? I have tried IDisposable interface for
my entities and GC.Collect() but it did not work.
If there is a lot of memory that isn't released after a call to GC.Collect(),
you should try using a memory profiler to see what objects are being left
in memory.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #11

P: n/a
Hi,

Do you know a profiler which is easy to use and understandable? I haven't
use a profiler before. Thanks...
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>To achieve reducing the amount of memory to the initial value which is
before opening the form, is there a way to get this expected result
after closing the form in C#? I have tried IDisposable interface for
my entities and GC.Collect() but it did not work.

If there is a lot of memory that isn't released after a call to
GC.Collect(), you should try using a memory profiler to see what objects
are being left in memory.

Best Regards,
Dustin Campbell
Developer Express Inc.


Oct 18 '06 #12

P: n/a
Adam,

Here are some links to the CLR Profiler. I haven't used it either.

..NET Framework 1.1
http://www.microsoft.com/downloads/d...displaylang=en

..NET Framework 2.0
http://www.microsoft.com/downloads/d...displaylang=en

Brian

Adam Right wrote:
Hi,

Do you know a profiler which is easy to use and understandable? I haven't
use a profiler before. Thanks...
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
To achieve reducing the amount of memory to the initial value which is
before opening the form, is there a way to get this expected result
after closing the form in C#? I have tried IDisposable interface for
my entities and GC.Collect() but it did not work.
If there is a lot of memory that isn't released after a call to
GC.Collect(), you should try using a memory profiler to see what objects
are being left in memory.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 18 '06 #13

P: n/a
Adam,

IDisposable is not a mechanism for reducing memory consumption. It is
used to cleanup unmanaged resources. Of course, cleaning up unmanaged
resources may reduce memory consumption, but it is doing so in the
unmanaged realm.

The best way to reduce memory consumption is not stop referencing
objects and let the GC work automatically. Keep in mind that even
after a garbage collection your application may not release memory back
to the OS. So if you're using task manager to measure the memory
consumption then you could get confusing results.

Brian

Adam Right wrote:
Hi,

To achieve reducing the amount of memory to the initial value which is
before opening the form, is there a way to get this expected result after
closing the form in C#? I have tried IDisposable interface for my entities
and GC.Collect() but it did not work.

Thanks...

"Adam Right" <ad**@right.comwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
Hi,

I am developing a financial application and you know that there are huge
amount of data in these similar aplications. I have a MDIChild form and
the other forms are opened in this form. For example, in a form i fetch
many rows from the database to show them to the user on the client
machine. After this process memory gets higher but when the user closes
that form the memory remains same amount.

I am using IDisposable interface for my entities and i use GC.Collect()
and GC.SuppressFinalize() method for my entities while they are being
disposed. But i cannot achieve to reduce the amount of the memory. How can
i reduce the amount of memory after the user closes the form? Is there a
way to get that result immediate after the user closes the form in C#?
Thanks...
Oct 18 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.