HighTechTalks DotNet Forums  

Memory Not Being Claimed

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Memory Not Being Claimed in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Mike King
 
Posts: n/a

Default Memory Not Being Claimed - 05-19-2006 , 08:24 AM






Can someone please explain why the DataSet isn't being collected? If I
un-comment "dr = null", then the memory is freed.

using System;
using System.Data;

class Class1
{
[STAThread]
static void Main(string[] args)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Customers");
dt.Columns.Add("custid", typeof(int));

for (int i=0; i<100000; ++i)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
//dr = null;
}

Console.WriteLine(System.GC.GetTotalMemory(true));
dt = null;
ds = null;
System.GC.Collect();
Console.WriteLine(System.GC.GetTotalMemory(true));
}
}



Reply With Quote
  #2  
Old   
Tasos Vogiatzoglou
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-19-2006 , 08:48 AM






I tried your example and it is collected (I tried it in a winform).


Reply With Quote
  #3  
Old   
Mike King
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-19-2006 , 09:14 AM



"Tasos Vogiatzoglou" <tvoglou (AT) gmail (DOT) com> wrote

Quote:
I tried your example and it is collected (I tried it in a winform).
Thanks for trying but can you do one more thing. I hope you're using VS
because now I think that's the key. If I create a "release build" there is
a big difference in memory usage between running the application by hitting
F5 and CTRL+F5. Can you verify the difference?




Reply With Quote
  #4  
Old   
Mike King
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-19-2006 , 09:16 AM



I forgot to mention that I'm using VS2003 with v1.1.

"Mike King" <emailMK (AT) excite (DOT) com> wrote

Quote:
Can someone please explain why the DataSet isn't being collected? If I
un-comment "dr = null", then the memory is freed.

using System;
using System.Data;

class Class1
{
[STAThread]
static void Main(string[] args)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Customers");
dt.Columns.Add("custid", typeof(int));

for (int i=0; i<100000; ++i)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
//dr = null;
}

Console.WriteLine(System.GC.GetTotalMemory(true));
dt = null;
ds = null;
System.GC.Collect();
Console.WriteLine(System.GC.GetTotalMemory(true));
}
}





Reply With Quote
  #5  
Old   
Tasos Vogiatzoglou
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-19-2006 , 09:24 AM



I think that the debugger causes this.

I can see the difference


Reply With Quote
  #6  
Old   
Mike King
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-19-2006 , 09:32 AM



"Tasos Vogiatzoglou" <tvoglou (AT) gmail (DOT) com> wrote

Quote:
I think that the debugger causes this.

I can see the difference

Thanks! I'm going to start a new thread over in
microsoft.public.vsnet.debugging.




Reply With Quote
  #7  
Old   
Phil Wilson
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-19-2006 , 12:49 PM



Don't bother! Debug mode is very different. It adds code to keep objects
around. Do you really want your unreferenced objects to disappear while
you're trying to look at them in debug mode? Debug mode would be impossible
if your objects were being collected. In general, GC in debug mode follows
scope rules.

You can't do any memory testing with debug builds!

--
Phil Wilson
[Microsoft MVP-Windows Installer]

"Mike King" <emailMK (AT) excite (DOT) com> wrote

Quote:
"Tasos Vogiatzoglou" <tvoglou (AT) gmail (DOT) com> wrote in message
news:1148045066.219380.186420 (AT) i39g2000cwa (DOT) googlegroups.com...
I think that the debugger causes this.

I can see the difference


Thanks! I'm going to start a new thread over in
microsoft.public.vsnet.debugging.




Reply With Quote
  #8  
Old   
Mike King
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-19-2006 , 01:09 PM



Thanks for the feedback.

I'm only talking about a "Release" build.

Yes, I would like the GC to collect unreferenced objects because if there is
not reference, then I cannot inspect it so what is the point. Also, how can
one inspect variables of a "Release" build anyway? I posted the following
to microsoft.public.vsnet.debugging.

#### BEGINNING OF MY PRIOR POST ####

I think I have figured it out. I _think_ Visual Studio 2003 is holding a
reference to reference types that are directly assigned to variables of a
method even if out of scope. The only exception is in a "Release" build and
the application is started without debugging. The code below is a simple
example of this behavior.

using System;

class Class1
{
[STAThread]
static void Main(string[] args)
{
WeakReference wr = null;
for (int i=0; i<1; ++i)
{
Uri uri = new Uri("http://domain.com");
wr = new WeakReference(uri);
}
System.GC.Collect();
Console.WriteLine(String.Format("IsAlive: {0}", wr.IsAlive));
Console.ReadLine();
}
}



"Phil Wilson" <pdjwilson (AT) nospam (DOT) cox.net> wrote

Quote:
Don't bother! Debug mode is very different. It adds code to keep objects
around. Do you really want your unreferenced objects to disappear while
you're trying to look at them in debug mode? Debug mode would be
impossible if your objects were being collected. In general, GC in debug
mode follows scope rules.

You can't do any memory testing with debug builds!

--
Phil Wilson
[Microsoft MVP-Windows Installer]

"Mike King" <emailMK (AT) excite (DOT) com> wrote in message
news:uWEAUj0eGHA.3468 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
"Tasos Vogiatzoglou" <tvoglou (AT) gmail (DOT) com> wrote in message
news:1148045066.219380.186420 (AT) i39g2000cwa (DOT) googlegroups.com...
I think that the debugger causes this.

I can see the difference


Thanks! I'm going to start a new thread over in
microsoft.public.vsnet.debugging.






Reply With Quote
  #9  
Old   
Tal
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-23-2006 , 03:24 AM



The GC process is a background process that runs on a different thread than your application.
Calling the GC to collect doesn't activate it explicitly.
Meaning you can't know when the GC will collect. You only know that it will happen. Multi threading kind of shit.

You should place an event handler to see when it does take place.

Reply With Quote
  #10  
Old   
Ben Voigt
 
Posts: n/a

Default Re: Memory Not Being Claimed - 05-24-2006 , 07:43 PM




<Tal> wrote

Quote:
The GC process is a background process that runs on a different thread
than your application.
Calling the GC to collect doesn't activate it explicitly.
Meaning you can't know when the GC will collect. You only know that it
will happen. Multi threading kind of shit.
You don't even know that. Finalizers don't have to run before the process
ends.

Take a look at "Managed Debugging Assistants" in the documentation:

"You can enable and disable MDAs by using a registry key, an environment
variable, or application configuration settings."
"By default, some MDAs are enabled when running the application attached to
a debugger, even without adding the registry key. "

Quote:
You should place an event handler to see when it does take place.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.