473,657 Members | 2,490 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 1338
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
3835
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
2696
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
3053
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
2918
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
3721
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
2250
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
25
2369
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
1368
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
4686
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
4525
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
8392
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
8305
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
8823
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...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4151
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
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.