1. Do we need to pass objects by ref, if we want to have the changes made in a calling function
2. And the same question for the objects passed by Remoting
3. Do we really gain performance in calling Dispose method of DataSet while leaving from a function(the dataset is no longer required)
If yes, then what does the Dispose do there Is it freeing the memory occupied by the objects If yes, then basic definition of GC itself gone right. And while the function got executed the DataSet objects declared within the function will also be removed from the reference list. Then is it required to do a Dispose while leaving a function. (I’m asking for DataSet and for managed objects)
4. The basic definition of GC is that it will frees memory occupied by objects and it will get executed when ever there is a need.
If you try doing a small programm that has a loop and if you fill a DataSet with values and call Clear method of the DataSet inside the loop, then if you see in the memory list in the “Task Manager” occupied by this program is varying as you fill and Clear the DataSet contents. If this is happening then I think the basic definition of GC it self gone right

Questions about Objects in C#
zomg123
JeffMN
Vikram Said:
1. Yes, for value types such as a string, int etc you would need to pass by ref. For objects this is not required.
------------------------------------------------------------
Just a correction Vikram, strings are not Value Types. Strings and Objects are Reference Types in .NET. However, int and other basic Types are value Types.
Karthikeyan, regarding your questuion about why your memory consumption changes when you are calling Clear() for the DataSet. It has a rather simple answer.
When you call the Clear() method for the DataSet does not clear out the memory occupies by the DataSet, nor it removes the DataSet object from the memory. The DataSet object contains references to other objects which it creates when you fill up the DataSet object, like the DataSetRowCollection and DataSetColumnCollection etc. Also when you are adding values to DataSet, you are actually creating two Array Objects and Adding them to Rows.
When you call Clear() on a DataSet, it clears itself, Frees its hold on objects like Rows and Object Arrays contained in it. When this happens the Garbage Collector runs for collecting the objects which are no longer required. But the DataSet object will not be removed from the memory.
I hope this tells why your memory consumption keeps on changing.
Regards,
Aleem
Uncle John
1. Ok
2. Remoted Objects : I'm deriving from MarshalByReference, even though I need to pass it by Ref, I think this is coz of the configuration setting how the object should behave like SingleCall or SingleTon.....In the case of SingleCall objects passed without ref is not reflecting. Am I it right
3. Calling a Dispose in DataSet : So, there is no use even when we call Dispose for DataSet. Am I Right
4. Calling a Clear() in DataSet : But as per my experiment program when I call the clear in loop The Memory occupied by DataSet is clearing.....how
If this is the case then we can call Clear() instead of Dispose() for DataSet.
If you the below program, I'm filling the DataSet and Calling Clear() inside a loop
In this case my If ONLY GC is handling the memory free up task then WHY DOES my program's memory consumption is varying while calling Clear().< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
using System;
using System.Data;
namespace MemoryTest
{
class Class1
{
[STAThread]
static void < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main (string[] args) {
DataSet ds = new DataSet();
ds.Tables.Add("Table1");
ds.Tables.Add("Table2");
ds.Tables[0].Columns.Add("c1");
ds.Tables[0].Columns.Add("c2");
ds.Tables[1].Columns.Add("c1");
ds.Tables[1].Columns.Add("c2");
while(true) {
ds.Tables[0].Rows.Add(new object[] { 1, 2 });
ds.Tables[1].Rows.Add(new object[] { 1, 2 });
ds.Clear();
//ds.Dispose();
}
}
}
}
cllard
Karthikeyan:
Personally, I don't think users of an application would notice what solution you choose - if even you can't determen what is better.
In general, you load data into a dataset, process it and close it. I don't think it really matters if your application uses that memory for just a second longer.
Also, I think calling the .Clean method itself also takes up a bit of peformance. Maybe not measured in memory, but in CPU usage.
If you are really worried about peformance, I think you should consider using another language, without object orientation. this is usually overhead itself.
hrh
Saurabh,
Calling Clear() may set internal references to null and/or, it may empty Collection objects. It won't set the DataSet object to null, so it will continue to take up memory, as will any of its internal value types and non-null references.
When you allow the GC to collect the DataSet object, all its memory (and internal objects) will be collected.
Hope that helps
-Chris
rakam
1. Yes, for value types such as a string, int etc you would need to pass by ref. For objects this is not required.
2. Yes this is true for Remoting as well but in case of objects passed by Remoting, there is the concept of MarshalByValue and MarshalByReference objects. Based on which one of the classes your remoting object derives from, the bahavior will change in terms of the way the object is transported across the wire. For MarshalByValue - the entire object is transported across the wire. For MarshalByRef - a proxy object is transported across the wire.
For a detailed discussion read:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconremotableobjects.asp
3. The Dispose method on a Dataset as well as a DataTable does nothing and it is not necessary to call them. The method has been included from the perspective of future design changes or for the purpose of derived classes which may use some unmanaged resource and need the dispose.
4. It is nondeterministic. It may have happened sometimes but you cant predict that the memory would get freed the same way everytime. The Garbage Collector has its own optimized logic and kicks in when required. In case you were filling the Dataset with enuff rows to reach a certain threshold which would trigger the GC to come in - then this may happen everytime.
Regards,
Vikram
Mirgle
With Warm Regards,
Karthikeyan
dcee
Calling clear explicitly clears all references - hence memory is reclaimed immediately. Although when an object goes out of scope (local object), it still lying there on the managed heap. And it will continue to live there unless the GC runs. So its not the same. You local DataSet will continue to live in memory, still referencing its value types untill the GC comes to check it. .. rest stuff I have already explained above.
Regards,
Saurabh Nandu
www.MasterCSharp.com
www.AksTech.com
Adam Friedman
Also I'd then pose the question back to you - why does he see difference in memory consumption when he explicitly calls Clear vs. Leaving it for the GC to collect it
Regards,
Saurabh Nandu
www.MasterCSharp.com
www.AksTech.com
MarioMor
Saurabh,
Your description of Value Types is slightly incorrect. Only local Value types are allocated on the stack, so any object members are allocated on the heap, and are garbage collected (you can verify this using the SOS debugger extension).
So that means for you Karthikeyan, calling Clear() will have the same effect for reference types (inherited from Object) and value types, since they are DataSet's members, and hence allocated on the heap.
Hope that helps
-Chris [MSFT]
Sir Codes Alot
Do I realy gain something when calling Clear() or Dispose() of DataSet at the end of a method(when the DataSet is declared locally)
1) Calling Dispose has no value on a DataSet. Since the DataSet object does not implement any code inside the Dispose method. Hence calling Dispose on a DataSet does nothing.
2) On the other hand calling clear has an effect. As earlier pointed out it clears the reference to all data tables, rows, columns that are contained within the dataset. Right Away. As well as it removes the data that is referenced.
(Hint: Always use ILDASM or .NET Reflector to find out what the .NET Methods actually perform)
Here's some more personal blurb on the questions you had..
Now when the variable goes out of scope generally. It still lies on the Managed Heap waiting for the GC to run. Untill the GC does not run the object will be not checked for references and cleared from memory. Also remember a DataSet is a very complex object made of various other kinds of ojbects. If any one of those objects referenced has a Finalizer defined. Then the DataSet will still live, untill its referenced objects are first removed (there is a seperate process to remove Finalized objects) and ultimately the DataSet gets removed. So it might even take couple of GC cycles to reclaim the DataSet objects memory.
Now when you call Clear, it explicitly removes all the data and objects referenced. Although the final memory gets reclaimed when the GC runs. But remember there are various Value Types that are used to compose a DataSet. memory from Value Types is released as soon as there is no reference to them (they are Stack Allocated). GC process is not used to reclaim memory for Value Types. Hence the immediate memory reclaimation that you see in Task Manager after calling Clear could be due to the fact that the memory held by Value Types are released.
Hope this clears this out for you.
Regards,
Saurabh Nandu
www.MasterCSharp.com
www.AksTech.com
Greysi
I understood but still I'm having a doubt in one scenario :< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Is calling Clear() in the end of a method is equals to not calling the Clear() (if the DataSet declared locally..... it gets out of scope when control leaves from that function ....so DataSet's contained objects will no longer referenced and even the value type's reference will also get removed )
So Is it like calling Clear() at end of a function will also has no effect
Thanks!
Regards
Karthikeyan
Elloco999
Aleem: In last < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Para you said "DataSet Frees its hold on objects like Rows and Object Arrays contained in it..... When this happens the Garbage Collector runs for collecting the objects which are no longer required"
My Understanding :
* DataSet's clear method will only remove the references got inside the DataSet and it won't frees up the memory and it doesn't influence the GC to get called except the removal of reference.
* Dispose method of DataSet does nothing.
So In my case you mean to say that GC is getting called very frequently ( for each iteration ....just coz it has more orphan objects)
Now don't consider my sample program........ and in general Is it like.....calling Clear() of DataSet at the end of function is indirectly useful in cleaning up the memory.
Or
When the control goes out of scope the DataSet and its container objects (like Rows and Object Arrays) will also be Orphaned and this method will also have the same effect in terms of memory consumption
In a Nutshell:
Do I realy gain something when calling Clear() or Dispose() of DataSet at the end of a method(when the DataSet is declared locally) < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks!
Regards
Karthikeyan
James D
2. Although I'm not 100% certain, I think the same rule applies for remoting too.
3. Dispose is a kind of "cleanup" function. Not necessarily memory cleanup. For example, if a DataSet opens a database connection, it can close it on Dispose. This is different from the regular garbage collection. If an object implements IDisposable, it is always a good idea to call Dispose when you're done with it.
4. A Garbage collector thread runs all the time - waking up at regular intervals and cleaning up memory. It can also run on demand when there's a need for cleanup. What you're seeing in the Task Manager is the implicit cleanup (not the on-demand one)
HTH