473,405 Members | 2,421 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,405 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 1556
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Philipp | last post by:
Hi, I am quite sure this question has answered here already several times, but I haven't found the answer yet. Anyway, here is the question again: I've got libraries (*.so) that are built with...
6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
14
by: Atara | last post by:
I know in C++ it is true. but what about VB .Net and its GarbageCollector ? For example, consider the following case. Does f2() needs to do any disposing code ? Public Sub f1() As...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: jonceramic | last post by:
Hi All, My apologies for asking something that I'm sure has been answered for. But, my google searching can't find a proper set of keywords. I would like to add "New..." or "other..." or...
12
by: Jordi | last post by:
I'm getting the following error: Software error: Can't locate object method "new" via package "A::B" at /path/file.cgi line 5. My code is basically this: #!/usr/bin/perl -w use strict; use...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.