473,789 Members | 2,806 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 #1
16 6736
LP,
It does, however you may be hurting the GC's ability to "do a good job",
rather then helping it.

For details see Rule #1 at:
http://blogs.msdn.com/ricom/archive/...29/271829.aspx
http://blogs.msdn.com/ricom/archive/.../02/40780.aspx

Just remember that "the collector is self-tuning so don't mess with it"!

Hope this helps
Jay

"LP" <lp@a.com> wrote in message
news:Oq******** ******@TK2MSFTN GP09.phx.gbl... 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 #2
LP,

The GC will keep up with object allocations, and collect them when it
requires. Like Jay stated, the GC is self-tuning based on your allocation
pattern, so it will find the best time to collect by itself.

Why do you feel you need to force garbage collections?

-Chris

--------------------

|
| LP,
| It does, however you may be hurting the GC's ability to "do a good job",
| rather then helping it.
|
| For details see Rule #1 at:
|
| >> http://blogs.msdn.com/ricom/archive/...29/271829.aspx
| >> http://blogs.msdn.com/ricom/archive/.../02/40780.aspx
|
| Just remember that "the collector is self-tuning so don't mess with it"!
|
| Hope this helps
| Jay
|
| "LP" <lp@a.com> wrote in message
| news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
| > 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 #3
LP
> Why do you feel you need to force garbage collections?
As stated before there is a loop creating new instances of a 3rd party
object each instances does some processing and outputs data to a report file
in PDF format. We tried having to work with one instance in the loop, but it
just screws up the formatting of the reports, it's pretty obvious it needs a
separate instance per report file.
As of now this program creates over 100 PDFs, it's possible that eventually
there will be much more, perhaps thousands. Performance of this application
is not a critical issue, since it's a batch process style app which runs on
scheduled basis.
So, We're Just trying to avoid potential memory problems (out of Memory
exceptions); we have seen a fair share of those.
""Chris Lyon [MSFT]"" <cl***@online.m icrosoft.com> wrote in message
news:SY******** ******@TK2MSFTN GXA03.phx.gbl.. .
LP,

The GC will keep up with object allocations, and collect them when it
requires. Like Jay stated, the GC is self-tuning based on your allocation
pattern, so it will find the best time to collect by itself.

Why do you feel you need to force garbage collections?

-Chris

--------------------

|
| LP,
| It does, however you may be hurting the GC's ability to "do a good job",
| rather then helping it.
|
| For details see Rule #1 at:
|
| >> http://blogs.msdn.com/ricom/archive/...29/271829.aspx
| >> http://blogs.msdn.com/ricom/archive/.../02/40780.aspx
|
| Just remember that "the collector is self-tuning so don't mess with it"!
|
| Hope this helps
| Jay
|
| "LP" <lp@a.com> wrote in message
| news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
| > 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 #4
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?

Dan

"LP" wrote:
Why do you feel you need to force garbage collections?

As stated before there is a loop creating new instances of a 3rd party
object each instances does some processing and outputs data to a report file
in PDF format. We tried having to work with one instance in the loop, but it
just screws up the formatting of the reports, it's pretty obvious it needs a
separate instance per report file.
As of now this program creates over 100 PDFs, it's possible that eventually
there will be much more, perhaps thousands. Performance of this application
is not a critical issue, since it's a batch process style app which runs on
scheduled basis.
So, We're Just trying to avoid potential memory problems (out of Memory
exceptions); we have seen a fair share of those.
""Chris Lyon [MSFT]"" <cl***@online.m icrosoft.com> wrote in message
news:SY******** ******@TK2MSFTN GXA03.phx.gbl.. .
LP,

The GC will keep up with object allocations, and collect them when it
requires. Like Jay stated, the GC is self-tuning based on your allocation
pattern, so it will find the best time to collect by itself.

Why do you feel you need to force garbage collections?

-Chris

--------------------

|
| LP,
| It does, however you may be hurting the GC's ability to "do a good job",
| rather then helping it.
|
| For details see Rule #1 at:
|
| >> http://blogs.msdn.com/ricom/archive/...29/271829.aspx
| >> http://blogs.msdn.com/ricom/archive/.../02/40780.aspx
|
| Just remember that "the collector is self-tuning so don't mess with it"!
|
| Hope this helps
| Jay
|
| "LP" <lp@a.com> wrote in message
| news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
| > 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 #5
> 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);


While I agree with everyone else that calling GC.Collect() isn't a good
idea, your call to Thread.Sleep isn't needed here. The GC activates on the
call to GC.Collect and WaitForPendingF inalizers() waits while finalizers are
executed and the finalizer queue cleared(however I don't know that it
ensures that those finalized object graphs are GC'd or not, that would
likely be much more important a question). By the time you are reaching the
sleep statement the garbage collection is complete.
Nov 17 '05 #6
LP
> 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.


Nov 17 '05 #7
LP,

Marina (regular to dotnet newsgroups) had about a year ago a problem with a
routine that had strange behaviour and memory.

Setting a "label.show " in here routine that we tried, started the GC. A kind
as if when the paint was given to the graphic adapter, the GC was doing its
job.

From that time I am not interested in it anymore because its behaviour
however have the idea that it is well done.

Cor
Nov 17 '05 #8
>
From that time I am not interested in it anymore because its behaviour
however have the idea that it is well done.


From that time I am not interested anymore when it is working, because thiss
behaviour gave me the idea that it was well done.

Nov 17 '05 #9
I would recommend against early memory/perf optimizations. It's best to
measure where the bottlenecks are and address them, than trying to do early
fixes which may cause other issues. Your GC.Collect loop, for example,
will cause early promotion of objects that would otherwise be considered
garbage. This will result in less memory being freed.

Rico Mariani has a great blog about performance, and talks a lot about the
GC:
http://blogs.msdn.com/ricom

In particular, I recommend:
http://blogs.msdn.com/ricom/archive/...29/271829.aspx
http://blogs.msdn.com/ricom/archive/...29/271829.aspx
http://channel9.msdn.com/wiki/defaul...Channel9.RicoM

Hope that helps
-Chris

--------------------

|
| > Why do you feel you need to force garbage collections?
| As stated before there is a loop creating new instances of a 3rd party
| object each instances does some processing and outputs data to a report
file
| in PDF format. We tried having to work with one instance in the loop, but
it
| just screws up the formatting of the reports, it's pretty obvious it
needs a
| separate instance per report file.
| As of now this program creates over 100 PDFs, it's possible that
eventually
| there will be much more, perhaps thousands. Performance of this
application
| is not a critical issue, since it's a batch process style app which runs
on
| scheduled basis.
| So, We're Just trying to avoid potential memory problems (out of Memory
| exceptions); we have seen a fair share of those.
|
|
| ""Chris Lyon [MSFT]"" <cl***@online.m icrosoft.com> wrote in message
| news:SY******** ******@TK2MSFTN GXA03.phx.gbl.. .
| > LP,
| >
| > The GC will keep up with object allocations, and collect them when it
| > requires. Like Jay stated, the GC is self-tuning based on your
allocation
| > pattern, so it will find the best time to collect by itself.
| >
| > Why do you feel you need to force garbage collections?
| >
| > -Chris
| >
| > --------------------
| >
| > |
| > | LP,
| > | It does, however you may be hurting the GC's ability to "do a good
job",
| > | rather then helping it.
| > |
| > | For details see Rule #1 at:
| > |
| > | >> http://blogs.msdn.com/ricom/archive/...29/271829.aspx
| > | >> http://blogs.msdn.com/ricom/archive/.../02/40780.aspx
| > |
| > | Just remember that "the collector is self-tuning so don't mess with
it"!
| > |
| > | Hope this helps
| > | Jay
| > |
| > | "LP" <lp@a.com> wrote in message
| > | news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
| > | > 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 #10

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
3390
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
2311
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
3816
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
1165
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
11905
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
9511
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
10412
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...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
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
9986
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...
1
7529
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.