473,803 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does this code Force Garbage Collector to Collect

LP
Hi,
Considering code below. Will it make GC to actually collect. One application
creates new instances of a class from 3rd party assembly in a loop (it has
to). That class doesn't have .Dispose or any similar method. I want to make
sure GC keeps up with the loop. My reasoning if Thread.Sleep(10 00) is
called; GC will take priority it do its work, right?

GC.Collect();
GC.WaitForPendi ngFinalizers();
System.Threadin g.Thread.Sleep( 1000);

Nov 17 '05
16 6738

"LP" <lp@a.com> wrote in message
news:Ot******** *****@TK2MSFTNG P12.phx.gbl...
Is this a .Net object? If so, if it is holding on to resources, it should
expose a Dispose method. If it is a COM object, are you calling
Marshal.Release to reduce the reference count once you have finished with

it?

Yes, it's a .NET object, but no unfortunately it doesn't expose .Dispose
method .
idea, your call to Thread.Sleep isn't needed here. The GC activates on
the
call to GC.Collect and WaitForPendingF inalizers() waits while finalizes

are
executed

I don't think it's true. GC runs on a separate thread with a priority
lower
than a main thread. Calling GC.collect() doesn't guarantee that GC will
start right away, it's my understanding you're telling runtime to increase
priority of GC, but when it will actually be executed depends on what is
currently running and/or scheduled to run, after runtime executes
important
tasks; then maybe GC.collect() is scheduled to execute or maybe not.
Calling
GC.Collect() is like kindly suggesting to runtime, maybe you should start
thinking about garbage collection, but when it will do is unknown.
Thread.Sleep() will stop current thread and give way to other scheduled
task
to execute which will hopefully be GC.collect (again it's not 100%, but
much
better chances)
I've seen enough arguments that GC is optimized to do its job when needed.
So I think it makes sense to comment out GC.Collect() , and monitor memory
usage.... I hope it will become more obvious if it's needed or not over
time.


The GC runs on a user's thread, not on a separate thread. Note that when the
GC runs all managed threads are suspended. The finalizer runs on a separate
thread and runs with a higher priority than normal.
The GC is activated when a certain managed heap threshold is reached, this
threashold depends on the size of the level 2 cache and is dynamically
adjusted according the usage pattern.
Calling GC.Collect will directly initiate a GC run, it's not like kindly
suggesting.

Willy,

Nov 17 '05 #11
Willy

A couple of corrections to your post:

|Note that when the GC runs all managed threads are suspended.

This is not true for Concurrent collections. See
http://blogs.msdn.com/clyon/archive/...08/226981.aspx.

| The GC is activated when a certain managed heap threshold is reached,

A collection can occur if an allocation fails, if memory pressure hits a
certain threshold, if the user induces a collection, and on appdomain
unloads, and various other scenarios.

| this threashold depends on the size of the level 2 cache and is
dynamically
| adjusted according the usage pattern.

As I stated above, the GC uses a variety of criteria to determine when to
collect. Your statement about level 2 cache isn't correct, although the GC
will tune itself based on allocation patterns. Users should not use L2
cache as indication of when the GC will perform a collection.

Hope that helps

-Chris

--------------------
|
|
| "LP" <lp@a.com> wrote in message
| news:Ot******** *****@TK2MSFTNG P12.phx.gbl...
| >> Is this a .Net object? If so, if it is holding on to resources, it
should
| >> expose a Dispose method. If it is a COM object, are you calling
| >> Marshal.Release to reduce the reference count once you have finished
with
| > it?
| >
| > Yes, it's a .NET object, but no unfortunately it doesn't expose .Dispose
| > method .
| >
| >> idea, your call to Thread.Sleep isn't needed here. The GC activates on
| >> the
| >> call to GC.Collect and WaitForPendingF inalizers() waits while finalizes
| > are
| >> executed
| > I don't think it's true. GC runs on a separate thread with a priority
| > lower
| > than a main thread. Calling GC.collect() doesn't guarantee that GC will
| > start right away, it's my understanding you're telling runtime to
increase
| > priority of GC, but when it will actually be executed depends on what is
| > currently running and/or scheduled to run, after runtime executes
| > important
| > tasks; then maybe GC.collect() is scheduled to execute or maybe not.
| > Calling
| > GC.Collect() is like kindly suggesting to runtime, maybe you should
start
| > thinking about garbage collection, but when it will do is unknown.
| > Thread.Sleep() will stop current thread and give way to other scheduled
| > task
| > to execute which will hopefully be GC.collect (again it's not 100%, but
| > much
| > better chances)
| > I've seen enough arguments that GC is optimized to do its job when
needed.
| > So I think it makes sense to comment out GC.Collect() , and monitor
memory
| > usage.... I hope it will become more obvious if it's needed or not over
| > time.
| >
|
| The GC runs on a user's thread, not on a separate thread. Note that when
the
| GC runs all managed threads are suspended. The finalizer runs on a
separate
| thread and runs with a higher priority than normal.
| The GC is activated when a certain managed heap threshold is reached,
this
| threashold depends on the size of the level 2 cache and is dynamically
| adjusted according the usage pattern.
| Calling GC.Collect will directly initiate a GC run, it's not like kindly
| suggesting.
|
| Willy,
|
|
|
|

Nov 17 '05 #12
Chris,

See inline ***

Willy.

""Chris Lyon [MSFT]"" <cl***@online.m icrosoft.com> wrote in message
news:hI******** ******@TK2MSFTN GXA03.phx.gbl.. .
Willy

A couple of corrections to your post:

|Note that when the GC runs all managed threads are suspended.

This is not true for Concurrent collections. See
http://blogs.msdn.com/clyon/archive/...08/226981.aspx.
*** I thought the difference between concurrent GC and non-concurrent GC was
that the former suspends the managed threads a few times for a short period
of time during a full (Gen2) collection, while the non-concurent flavor
suspends all threads (by calling SuspendEE) for the full duration of the GC,
calling RestartEE when done. I also thought that for a Gen0 or Gen1
collection there is no difference between the flavors, that means all
threads are suspended for the duration of the GC run.
So it was my understanding the whenever the GC effectively runs, be it for a
short period during full collects or for the whole collection, all managed
threads were suspended. Am I realy that wrong?
If I'm wrong, would'nt it mean the GC runs on a dedicated thread? Honestly I
never noticed this.
| The GC is activated when a certain managed heap threshold is reached,

A collection can occur if an allocation fails, if memory pressure hits a
certain threshold, if the user induces a collection, and on appdomain
unloads, and various other scenarios.
*** I know there are more criteria to trigger a collection, and the Gen
thresholds are only one of them.
| this threashold depends on the size of the level 2 cache and is
dynamically
| adjusted according the usage pattern.

As I stated above, the GC uses a variety of criteria to determine when to
collect. Your statement about level 2 cache isn't correct, although the
GC
will tune itself based on allocation patterns. Users should not use L2
cache as indication of when the GC will perform a collection.


*** I know there are more heuristics to determine the Gen0 heap size, but
according codepremium [1] the L2 cache size is one the parameters used to
set the initial size of the Gen0.

[1]
/codepremium/source/windows server 2003/winsrvr03 sp1
rc1(1289)/com/netfx/src/clr/vm/gcport.cpp
Nov 17 '05 #13
LP
I am learning a lot about GC from this discussions and links provided in
this thread. Thank you all.
I do have one more question; how can I monitor *real time* when GC starts
running and what thread. In general how one monitors what application is
doing what threads are running, how much memory is consumed, etc.
Do you use some kind of special tool?
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Chris,

See inline ***

Willy.

""Chris Lyon [MSFT]"" <cl***@online.m icrosoft.com> wrote in message
news:hI******** ******@TK2MSFTN GXA03.phx.gbl.. .
Willy

A couple of corrections to your post:

|Note that when the GC runs all managed threads are suspended.

This is not true for Concurrent collections. See
http://blogs.msdn.com/clyon/archive/...08/226981.aspx.
*** I thought the difference between concurrent GC and non-concurrent GC

was that the former suspends the managed threads a few times for a short period of time during a full (Gen2) collection, while the non-concurent flavor
suspends all threads (by calling SuspendEE) for the full duration of the GC, calling RestartEE when done. I also thought that for a Gen0 or Gen1
collection there is no difference between the flavors, that means all
threads are suspended for the duration of the GC run.
So it was my understanding the whenever the GC effectively runs, be it for a short period during full collects or for the whole collection, all managed
threads were suspended. Am I realy that wrong?
If I'm wrong, would'nt it mean the GC runs on a dedicated thread? Honestly I never noticed this.
| The GC is activated when a certain managed heap threshold is reached,

A collection can occur if an allocation fails, if memory pressure hits a
certain threshold, if the user induces a collection, and on appdomain
unloads, and various other scenarios.

*** I know there are more criteria to trigger a collection, and the Gen
thresholds are only one of them.
| this threashold depends on the size of the level 2 cache and is
dynamically
| adjusted according the usage pattern.

As I stated above, the GC uses a variety of criteria to determine when to collect. Your statement about level 2 cache isn't correct, although the
GC
will tune itself based on allocation patterns. Users should not use L2
cache as indication of when the GC will perform a collection.


*** I know there are more heuristics to determine the Gen0 heap size, but
according codepremium [1] the L2 cache size is one the parameters used to
set the initial size of the Gen0.

[1]
/codepremium/source/windows server 2003/winsrvr03 sp1
rc1(1289)/com/netfx/src/clr/vm/gcport.cpp

Nov 17 '05 #14
Hi Willy

--------------------
| *** I thought the difference between concurrent GC and non-concurrent GC
was
| that the former suspends the managed threads a few times for a short
period
| of time during a full (Gen2) collection, while the non-concurent flavor
| suspends all threads (by calling SuspendEE) for the full duration of the
GC,
| calling RestartEE when done. I also thought that for a Gen0 or Gen1
| collection there is no difference between the flavors, that means all
| threads are suspended for the duration of the GC run.
| So it was my understanding the whenever the GC effectively runs, be it
for a
| short period during full collects or for the whole collection, all
managed
| threads were suspended. Am I realy that wrong?
| If I'm wrong, would'nt it mean the GC runs on a dedicated thread?
Honestly I
| never noticed this.

No, the GC doesn't have a dedicated thread, sorry if I implied that.
You are right: concurrent gc only applies to gen2 collections, and that it
suspends managed threads for only part of the collection, as opposed to
non-concurrent which suspends them for the entire collection.

Maoni has an in-depth blog entry about concurrent gc:
http://blogs.msdn.com/maoni/archive/...25/234273.aspx

Also, Gen0 has a larger budget under concurrent gc than non-concurrent.

-Chris

Nov 17 '05 #15
Hi LP

| I do have one more question; how can I monitor *real time* when GC starts
| running and what thread. In general how one monitors what application is
| doing what threads are running, how much memory is consumed, etc.
| Do you use some kind of special tool?

You can use Performance Counters to determine the amount of memory
consumed, and the CLR Profiler to glean certain information about the GC
heap.

As for real time analysis, try the SOS extension to WinDBG.

-Chris

Nov 17 '05 #16
Chris,
inline ***

Thanks,
Willy.

""Chris Lyon [MSFT]"" <cl***@online.m icrosoft.com> wrote in message
news:Rc******** ******@TK2MSFTN GXA03.phx.gbl.. .
No, the GC doesn't have a dedicated thread, sorry if I implied that. *** You didn't imply that, I did, suposed I was wrong and the threads were
not suspended.
You are right: concurrent gc only applies to gen2 collections, and that it
suspends managed threads for only part of the collection, as opposed to
non-concurrent which suspends them for the entire collection.

Maoni has an in-depth blog entry about concurrent gc:
http://blogs.msdn.com/maoni/archive/...25/234273.aspx
*** One of my favorite readings just like your blog stuff.
Also, Gen0 has a larger budget under concurrent gc than non-concurrent.
*** That's right, it's a little unfortunate that you only can get this on MP
boxes though.
-Chris

Nov 17 '05 #17

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

Similar topics

6
810
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
28
3395
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all images that have been used and are going to be closed. This is how ever no way to reduce the memory consumption. I have noticed , using the task manager, that garbage collector doesn't actually do any collections unless the computer becomes low...
10
2313
by: Ray5531 | last post by:
Is there a way in C# to stop garbage collector from killing our object automatically.I'd like to kill it myself?? Is there away to to do so? Thanks
4
13871
by: IcedCrow | last post by:
I know I read everywhere that you "can't" force the garbage collector to run, and that you really have no control when it runs. However as a programmer I know that there is always a way around something. So I ask you, how do you force the garbage collector to run and clean up?
13
3818
by: Mingnan G. | last post by:
Hello everyone. I have written a garbage collector for standard C++ application. It has following main features. 1) Deterministic Finalization Providing deterministic finalization, the system can manage resources as well as objects. The programming style is clear and easy, conforming to RAII (Resource Acquisition Is Initialization) idiom of C++ programmers. The memory usage is very efficient, acyclic garbage is
4
3005
by: daniel.internet | last post by:
Hello, I got one question, that may seems trivial to you, but I really dont understand it. Let's say we got this code: function ajax() { var obj; obj = new ActiveXObject("Microsoft.XMLHTTP"); obj.onreadystatechange = function ()
2
1166
by: John Smith | last post by:
Does .Net has garbage collector or I have to free (Set myVariable = Nothing) each object I created with "New"?
350
11917
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
0
9703
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
9565
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
10550
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
10295
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
10069
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
9125
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
5501
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...
1
4275
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
3799
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.