471,348 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

"new"

Hi,

I know what "new" (and "delete") does in C++, which I've used for
years. I have worked with C# for some time, too, but ok, there are
some curious things about "new" in C# that I still don't grab. For
instance, independently on when the garbage collector kicks in, the
following code should make the amount of memory assigned to my
application grow from A to B and from B to C, right? Well, it doesn't.

At point A: 21488 KB.
At point B: 23488 KB.
At point C: 22288 KB.

(According to the Windows Task Manager, looking at the "Processes"
tab.)

I know the code is not useful, but it should not matter. I'm trying to
understand why, sometimes, "new" in C# does not make the amount of
memory assigned to my application grow.

Thank you,
Mochuelo

PS: BTW, is there a way to know, from the program itself, how much
memory is being assigned to the application at any time? How about
memory assigned to an object?
--------------------
MySqlConnection con1;

// Point A.

for (k=0;k<10000;k++)
{
con1 =new
MySqlConnection("Database=db1;Server=localhost;Uid =exampleusr;Password=examplepwd");
}

// Point B.

for (k=0;k<10000;k++)
{
con1 =new
MySqlConnection("Database=db1;Server=localhost;Uid =exampleusr;Password=examplepwd");
}

// Point C.
--------------------

Feb 12 '06 #1
9 1465
"Mochuelo" <cucafera@RE_MO_VE_THIStelefonica.net> a écrit dans le message de
news: ku********************************@4ax.com...

First of all, the Task Manager cannot be relied on to tell you how much
memory is allocated by your program; all it tells you is how much memory the
Windows memory manager has allocated, not necessarily your process.

| for (k=0;k<10000;k++)
| {
| con1 =new
|
MySqlConnection("Database=db1;Server=localhost;Uid =exampleusr;Password=examplepwd");
| }

All this code does is allocate an object, then allocate another object to
the same reference, allowing the previous one to be garbage collected. So,
in effect, you only ever take enough memory for one object.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 12 '06 #2
Mochuelo wrote:
Hi,

I know what "new" (and "delete") does in C++, which I've used for
years. I have worked with C# for some time, too, but ok, there are
some curious things about "new" in C# that I still don't grab. For
instance, independently on when the garbage collector kicks in, the
following code should make the amount of memory assigned to my
application grow from A to B and from B to C, right? Well, it doesn't.


I think it shouldn't necessarly make it grow from A to B to C. At each
step only one MySqlConnection is referenced by a variable. All the
others could already be deleted again right after each new.

If you stored the objects in an array or ArrayList, the memory usage
would have to grow, because a reference would be held to each object.

hth,
Max
Feb 12 '06 #3
On Sun, 12 Feb 2006 18:15:39 -0000, "Joanna Carter [TeamB]"
<jo****@not.for.spam> wrote:
"Mochuelo" <cucafera@RE_MO_VE_THIStelefonica.net> a écrit dans le message de
news: ku********************************@4ax.com...

First of all, the Task Manager cannot be relied on to tell you how much
memory is allocated by your program; all it tells you is how much memory the
Windows memory manager has allocated, not necessarily your process.
Can you please tell me a reliable way to know, during runtime, how
much memory is being allocated to my process? I'm using Visual Studio
2005.
| for (k=0;k<10000;k++)
| {
| con1 =new
|
MySqlConnection("Database=db1;Server=localhost;Ui d=exampleusr;Password=examplepwd");
| }

All this code does is allocate an object, then allocate another object to
the same reference, allowing the previous one to be garbage collected. So,
in effect, you only ever take enough memory for one object.


This is what I suspected, although it is hard for me to digest, coming
from C++. Do we agree that the following code, in C++, makes the
memory assigned to my process grow from A to B and again from B to C?

---------------
MyClass *mc;
// Point A.
mc =new MyClass;
// Point B.
mc =new MyClass;
// Point C.
---------------

Thanks a lot,
Mochuelo.

Feb 12 '06 #4
One thing to keep in mind when doing any .NET programming is that you cannot
have memory leaks. The Garbage Collector will release any memory that is no
longer referenced so when you assign a variable to a new object instance the
old one can then be GC'd if there are no other ref's to it.

In C++ there is not GC so when you loose the reference to a chunk of memory
that memory just hangs around until your application closes. Thus, you
have, a memory leak.

Also, take a look at the garbage collection class and see if that can give
you some insight into how much memory is in use.

Hope this helps.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Mochuelo" <cucafera@RE_MO_VE_THIStelefonica.net> wrote in message
news:38********************************@4ax.com...
On Sun, 12 Feb 2006 18:15:39 -0000, "Joanna Carter [TeamB]"
<jo****@not.for.spam> wrote:
"Mochuelo" <cucafera@RE_MO_VE_THIStelefonica.net> a écrit dans le message
de
news: ku********************************@4ax.com...

First of all, the Task Manager cannot be relied on to tell you how much
memory is allocated by your program; all it tells you is how much memory
the
Windows memory manager has allocated, not necessarily your process.


Can you please tell me a reliable way to know, during runtime, how
much memory is being allocated to my process? I'm using Visual Studio
2005.
| for (k=0;k<10000;k++)
| {
| con1 =new
|
MySqlConnection("Database=db1;Server=localhost;U id=exampleusr;Password=examplepwd");
| }

All this code does is allocate an object, then allocate another object to
the same reference, allowing the previous one to be garbage collected. So,
in effect, you only ever take enough memory for one object.


This is what I suspected, although it is hard for me to digest, coming
from C++. Do we agree that the following code, in C++, makes the
memory assigned to my process grow from A to B and again from B to C?

---------------
MyClass *mc;
// Point A.
mc =new MyClass;
// Point B.
mc =new MyClass;
// Point C.
---------------

Thanks a lot,
Mochuelo.

Feb 12 '06 #5
On Sun, 12 Feb 2006 18:15:39 -0000, "Joanna Carter [TeamB]"
<jo****@not.for.spam> wrote:
First of all, the Task Manager cannot be relied on to tell you how much
memory is allocated by your program; all it tells you is how much memory the
Windows memory manager has allocated, not necessarily your process.


Wait a minute, you say that the "Processes" tab in the Windows Task
Manager does not say how much memory is being allocated to each
process at any time? The name of the tab suggests so, doesn't it?

Best,

Feb 12 '06 #6

"Mochuelo" <cucafera@RE_MO_VE_THIStelefonica.net> wrote in message
news:no********************************@4ax.com...
| On Sun, 12 Feb 2006 18:15:39 -0000, "Joanna Carter [TeamB]"
| <jo****@not.for.spam> wrote:
|
| >First of all, the Task Manager cannot be relied on to tell you how much
| >memory is allocated by your program; all it tells you is how much memory
the
| >Windows memory manager has allocated, not necessarily your process.
|
| Wait a minute, you say that the "Processes" tab in the Windows Task
| Manager does not say how much memory is being allocated to each
| process at any time? The name of the tab suggests so, doesn't it?
|
| Best,
|

Sure it does, the values exposed by Taskman are nothing else than the
"Performance counter" values, the only problem is that the column names are
a bit confusing, so does "Mem Usage" is in reality the "Working Set" size
while "VM Size" is the "Private Bytes" count.

Willy.

Feb 12 '06 #7

"Mochuelo" <cucafera@RE_MO_VE_THIStelefonica.net> wrote in message
news:ku********************************@4ax.com...
| Hi,
|
| I know what "new" (and "delete") does in C++, which I've used for
| years. I have worked with C# for some time, too, but ok, there are
| some curious things about "new" in C# that I still don't grab. For
| instance, independently on when the garbage collector kicks in, the
| following code should make the amount of memory assigned to my
| application grow from A to B and from B to C, right? Well, it doesn't.
|
| At point A: 21488 KB.
| At point B: 23488 KB.
| At point C: 22288 KB.
|
| (According to the Windows Task Manager, looking at the "Processes"
| tab.)
|
| I know the code is not useful, but it should not matter. I'm trying to
| understand why, sometimes, "new" in C# does not make the amount of
| memory assigned to my application grow.
|

The GC only collects managed memory, that is memory taken by the "managed
object" instance, in your case there is some "unmanaged" memory involved
when creating an instance of 'MySqlConnection'.
This unmanaged memory is or, deleted at a later time (when the finalizer
runs), or is deleted when you explicitely dispose the instance for
IDisposable objects, or is deleted by the owner of the underlying resource
instance (memory or other).


| Thank you,
| Mochuelo
|
| PS: BTW, is there a way to know, from the program itself, how much
| memory is being assigned to the application at any time? How about
| memory assigned to an object?
|

Yes, you can use the Performance counters through the System.Diagnostic
classes, there is no reliable way to get at the size of 'managed objects'
from managed code, and IMO there is no need to either.
Willy.
Feb 12 '06 #8
Mochuelo,

Let me take a slightly different approach to your question.

The zen of any garbage collected general purpose environment is that you
don't even focus on these sorts of questions. You just use the tool to
implement the solution, and you don't concern yourself with memory usage
or performance unless and until there is a demonstrated problem in those
areas. At that point you use the debugging and profiling tools to find
the TRUE bottleneck in the particular situation. You assumptions about
where the problem is will almost always been wrong anyway.

Yes, you want to write efficient code, but what you're after is cost
effective solutions in the customer's problem domain, not the mythical
perfect solution. The GC is there so you don't have to fuss about
memory management. It's the product of bigger brains than mine or
yours, and unless you are putting megabytes of matrices of numbers into
memory or something, you VERY seldom need be concerned with things like
which collection class uses 2% less memory or which type of looping
construct is 2% faster. Indeed, there are few such unqualified rules of
thumb anyway. Always assuming, of course that you're using .NET for its
intended purpose -- line-of-business apps and services -- not game
programming or something.

I've noticed that people coming from a C++ background in particular have
a problem "letting go" in this area. They have been trained to feel
emasculated or at least neglectful if they don't aggressively
micro-manage memory and performance issues. The whole idea behind .NET
is to free you from this grunt work so you can focus on delivering
functionality to your customers.

Best,

--Bob

Mochuelo wrote:
Hi,

I know what "new" (and "delete") does in C++, which I've used for
years. I have worked with C# for some time, too, but ok, there are
some curious things about "new" in C# that I still don't grab. For
instance, independently on when the garbage collector kicks in, the
following code should make the amount of memory assigned to my
application grow from A to B and from B to C, right? Well, it doesn't.

At point A: 21488 KB.
At point B: 23488 KB.
At point C: 22288 KB.

(According to the Windows Task Manager, looking at the "Processes"
tab.)

I know the code is not useful, but it should not matter. I'm trying to
understand why, sometimes, "new" in C# does not make the amount of
memory assigned to my application grow.

Thank you,
Mochuelo

PS: BTW, is there a way to know, from the program itself, how much
memory is being assigned to the application at any time? How about
memory assigned to an object?
--------------------
MySqlConnection con1;

// Point A.

for (k=0;k<10000;k++)
{
con1 =new
MySqlConnection("Database=db1;Server=localhost;Uid =exampleusr;Password=examplepwd");
}

// Point B.

for (k=0;k<10000;k++)
{
con1 =new
MySqlConnection("Database=db1;Server=localhost;Uid =exampleusr;Password=examplepwd");
}

// Point C.
--------------------

Feb 13 '06 #9
C.C.,

Your statement is only technically true. The GC will see to it that all
memory is *ultimately* freed, but that doesn't guarantee that
unnecessary resources won't be consumed. "Ultimately" could mean "on
program exit", after all.

You have to work at it, but you can certainly construct sloppy systems
in .NET that are resource pigs and that use progressively more memory
until they become unusably slow -- even if they don't technically crash.

This would be the result of very poor object designs and grossly
uninformed design choices, however, not because of a failure to obsess
over whether memory in fact appears to be freed after each reference
goes out of scope.

--Bob

C.C. (aka Me) wrote:
One thing to keep in mind when doing any .NET programming is that you cannot
have memory leaks.

Feb 13 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Philipp | last post: by
6 posts views Thread by dpr | last post: by
30 posts views Thread by Medvedev | last post: by
1 post views Thread by Ronak mishra | last post: by

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.