473,837 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory question

Suppose I have a dataset, dsMain, that is already filled with data. What
then, is the affect of this:

DataSet ds1 = dsMain;

Does this cause ds1 to simply become a pointer to dsMain? If so, how is
this code different:

DataSet ds1 = new DataSet();
ds1 = dsMain;

Does ds1 now contain a copy of dsMain? Also, should I call ds1.Dispose() in
both examples, or only in the second one?

TIA,

Mike Rodriguez
Nov 17 '05 #1
11 1346
Michael,

When you do this:

DataSet ds1 = dsMain;

You are assigning ds1 a reference to whatever dsMain is pointing to. A
copy is not made.

When you do this:

DataSet ds1 = new DataSet();
ds1 = dsMain;

You create a new data set, and then that dataset becomes eligible for
garbage collection once you assign the reference dsMain to ds1. In both
instances you are only holding a reference, a copy is not made.

Technically, you should call Dispose on anything that implements
IDisposable. However, I believe that the data set only has that
implementation because of it deriving from MarshalByValueC omponent, and it
doesn't really do anything (there is no override of Dispose(boolean ) that I
can see).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Michael Rodriguez" <mi**@nospamfor me.com> wrote in message
news:ed******** ******@tk2msftn gp13.phx.gbl...
Suppose I have a dataset, dsMain, that is already filled with data. What
then, is the affect of this:

DataSet ds1 = dsMain;

Does this cause ds1 to simply become a pointer to dsMain? If so, how is
this code different:

DataSet ds1 = new DataSet();
ds1 = dsMain;

Does ds1 now contain a copy of dsMain? Also, should I call ds1.Dispose()
in both examples, or only in the second one?

TIA,

Mike Rodriguez

Nov 17 '05 #2
Hi Michael,
DataSet ds1 = dsMain;

Does this cause ds1 to simply become a pointer to dsMain?
Yep.
If so, how is this code different:

DataSet ds1 = new DataSet();
ds1 = dsMain;

Does ds1 now contain a copy of dsMain?
Nope, still a pointer to dsMain. The blank DataSet instance you have created
has now no references and will be reclaimed by the Garbage Collector.
Also, should I call ds1.Dispose() in both examples, or only in the second
one?
You should not call Dispose() at all. Dispose() does not release managed
memory, Dispose() makes sure that unmanaged resources are released properly.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Michael Rodriguez" <mi**@nospamfor me.com> wrote in message
news:ed******** ******@tk2msftn gp13.phx.gbl... Suppose I have a dataset, dsMain, that is already filled with data. What
then, is the affect of this:

DataSet ds1 = dsMain;

Does this cause ds1 to simply become a pointer to dsMain? If so, how is
this code different:

DataSet ds1 = new DataSet();
ds1 = dsMain;

Does ds1 now contain a copy of dsMain? Also, should I call ds1.Dispose()
in both examples, or only in the second one?

TIA,

Mike Rodriguez


Nov 17 '05 #3
> You should not call Dispose() at all.

Whoa! DataSet implements IDisposable. My understanding is that it is
"good practice" to call Dispose() on any IDisposable object that you
are discarding. So, if I have heard right, "good practice" (as useless
as it might be) in this case would be:

DataSet ds1 = new DataSet();
ds1.Dispose();
ds1 = dsMain;

As Nicholas pointed out, it does not appear that DataSet does anything
in its Dispose() method, but as a caller I can't rely on that. DataSet
in .NET 2.0 (or 2.1, or 3.0) might decide to add some code to its
Dispose method. According to the contract, I _should_ be calling
Dispose() to dispose of a DataSet (in those situations in which I can
know that I hold the last reference to it). To do anything else would
be to peek behind the veil, and it may come back to burn me later.

Or do I have this all wrong?

Nov 17 '05 #4
Bruce,

You do not have this wrong, I would say that you elaborated very well
the reason why you ^should^ call Dispose. Dmytro is wrong in this case,
IMO.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
You should not call Dispose() at all.


Whoa! DataSet implements IDisposable. My understanding is that it is
"good practice" to call Dispose() on any IDisposable object that you
are discarding. So, if I have heard right, "good practice" (as useless
as it might be) in this case would be:

DataSet ds1 = new DataSet();
ds1.Dispose();
ds1 = dsMain;

As Nicholas pointed out, it does not appear that DataSet does anything
in its Dispose() method, but as a caller I can't rely on that. DataSet
in .NET 2.0 (or 2.1, or 3.0) might decide to add some code to its
Dispose method. According to the contract, I _should_ be calling
Dispose() to dispose of a DataSet (in those situations in which I can
know that I hold the last reference to it). To do anything else would
be to peek behind the veil, and it may come back to burn me later.

Or do I have this all wrong?

Nov 17 '05 #5
Hi,
"Michael Rodriguez" <mi**@nospamfor me.com> wrote in message
news:ed******** ******@tk2msftn gp13.phx.gbl...
Suppose I have a dataset, dsMain, that is already filled with data. What
then, is the affect of this:

DataSet ds1 = dsMain;

Does this cause ds1 to simply become a pointer to dsMain? If so, how is
this code different:
Yes, both will hold a reference to the same instance.
DataSet ds1 = new DataSet();
ds1 = dsMain;


the dataset instance previously being reference by ds1 is not longer
referenced and hence becomes marked to be GC.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #6
Thanks to everyone for all the speedy replies!
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:OG******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi,
"Michael Rodriguez" <mi**@nospamfor me.com> wrote in message
news:ed******** ******@tk2msftn gp13.phx.gbl...
Suppose I have a dataset, dsMain, that is already filled with data. What
then, is the affect of this:

DataSet ds1 = dsMain;

Does this cause ds1 to simply become a pointer to dsMain? If so, how is
this code different:


Yes, both will hold a reference to the same instance.
DataSet ds1 = new DataSet();
ds1 = dsMain;


the dataset instance previously being reference by ds1 is not longer
referenced and hence becomes marked to be GC.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #7
Hi,

Then the question is why does it implement it?
It's MarshalByValueC omponent the one you implements it and DataSet derive
from it.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eK******** ******@TK2MSFTN GP14.phx.gbl...
Bruce,

You do not have this wrong, I would say that you elaborated very well
the reason why you ^should^ call Dispose. Dmytro is wrong in this case,
IMO.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
You should not call Dispose() at all.


Whoa! DataSet implements IDisposable. My understanding is that it is
"good practice" to call Dispose() on any IDisposable object that you
are discarding. So, if I have heard right, "good practice" (as useless
as it might be) in this case would be:

DataSet ds1 = new DataSet();
ds1.Dispose();
ds1 = dsMain;

As Nicholas pointed out, it does not appear that DataSet does anything
in its Dispose() method, but as a caller I can't rely on that. DataSet
in .NET 2.0 (or 2.1, or 3.0) might decide to add some code to its
Dispose method. According to the contract, I _should_ be calling
Dispose() to dispose of a DataSet (in those situations in which I can
know that I hold the last reference to it). To do anything else would
be to peek behind the veil, and it may come back to burn me later.

Or do I have this all wrong?


Nov 17 '05 #8
Ignacio,

The reason it implements it through the base class is that the designers
of the framework didn't know whether or not the components that would be
created later would have the need for disposing. Instead of leaving it up
to the individual component vendors, they felt it best that they just bake
it into the base class for all components (the Component class and the
MarshalByValueC omponent class).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

Then the question is why does it implement it?
It's MarshalByValueC omponent the one you implements it and DataSet derive
from it.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eK******** ******@TK2MSFTN GP14.phx.gbl...
Bruce,

You do not have this wrong, I would say that you elaborated very well
the reason why you ^should^ call Dispose. Dmytro is wrong in this case,
IMO.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
You should not call Dispose() at all.

Whoa! DataSet implements IDisposable. My understanding is that it is
"good practice" to call Dispose() on any IDisposable object that you
are discarding. So, if I have heard right, "good practice" (as useless
as it might be) in this case would be:

DataSet ds1 = new DataSet();
ds1.Dispose();
ds1 = dsMain;

As Nicholas pointed out, it does not appear that DataSet does anything
in its Dispose() method, but as a caller I can't rely on that. DataSet
in .NET 2.0 (or 2.1, or 3.0) might decide to add some code to its
Dispose method. According to the contract, I _should_ be calling
Dispose() to dispose of a DataSet (in those situations in which I can
know that I hold the last reference to it). To do anything else would
be to peek behind the veil, and it may come back to burn me later.

Or do I have this all wrong?



Nov 17 '05 #9
This brings up an interesting design problem, both from my point of
view and from Microsoft's point of view. Let's look at it from MS's
point of view, because I think it's clearer in that case.

If MS publishes a class (such as DataSet) that implements IDisposable,
they're leaving their options open. Perhaps you really have to call
Dispose() on instances of the class, and perhaps you don't. However,
you _should_, because you never know what might happen to the class
implementation in the future. However, from MS's point of view, the
cost is that all users of that class now have to worry about disposal.
How annoying.

Furthermore, if they ever change a class that must be Dispose()d so
that it's now totally managed code and does not need to be Dispose()d,
they can never remove the IDisposable implementation from that class or
they'll break all of the code out there that took pains to properly
dispose of objects of that class. So, they're stuck for all eternity
having the class be IDisposable when it doesn't really need to be.

However, if MS publishes a class that does not implement IDisposable,
then they are, in effect guaranteeing that for all time the class will
be totally managed and will never need to be disposed. If they add
IDisposable later, they're breaking the contract and legacy code may
not properly dispose of instances of that class under the new
Framework. Bummer.

Perhaps this is why documentation for IDisposable discourages us from
making Dispose methods that _have_ to be called in order that an object
be GC'd. For example, this is why it's "bad style" to use Dispose() to
unhook an object from static events, because then if Dispose() is never
called then the object will never be GC'd. (Not that I never abuse
Dispose() in this way... just that I shouldn't :-)

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
3867
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
6
2703
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the memory allocation algorithm, but for gathering some statistics and to find memory leaks.) This seems to work. However, I noticed that my replacing delete operator is not called
20
3078
by: Jonas | last post by:
Hi, I'm 99 % sure that Standard C guarantees to do a memory move inside realloc() in case the new, returned memory block (address) is different than the original one. Can any C expert confirm this to me, please? Thanks, Jonas PS. I using C90, not C99--if it makes a difference.
8
2937
by: vikram | last post by:
i have series of questions 1.How a c program is loaded in memory i mean the whats is the structure that the code segment?? data segment?? 2.When you say const int *p; where is p stored in the memory?? what happens internal so that its a read only. 3. when declared volatile int *p where exactly in the memory it is stored.
30
3768
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
18
2280
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
25
2399
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My memory profile shows that those instances survive 3 rounds of GC collections - it's not what I expected. In my real code, CMyClass occupies big amount of memory and they all share one stance of another class that I don't have enough memory hold...
2
1375
by: Ashish | last post by:
hi all, I have been doing some performance testing of a asp_net website, to be hosted on a shared server .. as far as i understand every page when accessed first time is compiled and loaded in the memory of aspnet_wp , so that would mean more distinct pages more the memory consumed by aspnet_wp, this is good since it would mean serving compiled copy of the page , which is good for speed.. another understanding is that once a page is...
7
4695
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now are the memory layout is contiguous? i.e all the character resides side by side just like array, and all Points side by side insede the
9
4545
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working on linux platform and my target is ARM processor. But i guess it should not matter. Actually i need to know both RAM & ROM usage.
0
9843
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10875
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10566
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9401
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5669
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5848
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4040
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.