473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Disposing Objects?

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
13 1910
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
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


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
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


"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
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


"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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Egghead | last post by:
Hi all, I'm just wondering that is any way to dispose all objects when my app is shut down? I mean I can use for.. next loop to unload all loaded form when I shut down my app. Thank you, ...
15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
4
by: MC D | last post by:
Question: If I have a class which has a property which is a collection of widget objects (an arrayList of widgets), and both the containter class and the widget class implement the IDisposable...
5
by: Mathias L. | last post by:
I have two questions for which I couldnt find answer: If I programaticaly close DialogForm (calling Close()), is it enough or do I have to dispose it as MS.NET help says? Also, in overriden...
4
by: sunitabalakrishna | last post by:
Hi, Could someone explain me if I need to implement the Dispose pattern on managed objects that hold objects which have longer life or is a singleton? Example - Class A holds Class Global...
7
by: Mariusz | last post by:
Hello, I have question regarding disposing objects in aspx/cs pages. If I create some objects/Arrays/ListArrays in pageLoad do I have to dispose them (by assigning the null value for objects that...
0
by: Gman | last post by:
Hi, Objective: Draw a grid on a bitmap and set this as a panel's image. (Rather than draw the grid directly on the panel and redraw repeatedly in the paint event.) Problem: It works fine....
4
by: Rob | last post by:
Hi all, I wrote myself a little function to do my own housekeeping, I can call it by using Dispose(object) Within this function there's a select case statement which will then, based on...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.