473,770 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What good is this automatic garbage collector?

joe
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 on memory. This is very foolish, and what good is a garbage collector
which doesn't collect the disposed objects when they aren't needed anymore?

Besides, calling CG.Collect() is usually avoided for performance and speed.
What else can i do?

PS: i wont hurt you to read this:
http://www.cs.tut.fi/~warp/MicrosoftComparingLanguages/

Jul 21 '05
28 3388
The GC runs more often than you think - just watch the CLR Performance
counters while your program is running, but the GC isn't meant to collect
unmanaged resources like file handles, DB connections and unmanaged memory,
the GC will also not collect non garbage, that is objects that have live
references. Non managed resources can explicitly and deterministical ly be
released by calling Dispose or Close or whatever method there is used to do
so. the FCL used the Dispose pattern to dispose of unmanaged resources, and
the languages (C# since v1 and VB in v2.0) help you to automate this pattern
by means of the using statement. If you fail to apply this design pattern
and if you are holding references to objects alive when they should not than
there is nothing the GC can do to reclaim memory, but this is not different
from the unmanaged world.
Note, there will be no swapping because non referenced objects aren't GC
collected. The OS will trigger an event that will force the GC to collect
when there is memory pressure. Note also that the garbage collector runs
more often than you thing.
Willy.

"joe" <jo*****@rot.of m.net> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
Nick,
Your reasoning is respected and welcomed. I don't hate or love something
like idiots, i simply point out the weak points.

It is true that CG is a great miracle in C# and java, and help the
programmer to concentrate on more critical aspects of his work, however,
when it comes to page faults, it exhibits its weak points.

You might say that because allocation or garbage collection is done only
when the computer becomes low on memory, you gain much higher performance
in your application. This can be true for small programs, however, when
some real big programs are executed on computers with low memory ( sth
like 256 MB) , then the amount of OS's page swapping and virtual memory
reading and writing really slows down the entire system. Just think, why
your OS is much faster after a clean restart than the time it is run for
long hours of application executing and playing with the virtual memory?

Well, at least there could be stricter algorithm implemented, which would
force the GC to come into play much sooner that it does now on low-memory
PCs.
"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:pI******** ************@co mcast.com...
PS: i wont hurt you to read this:
http://www.cs.tut.fi/~warp/MicrosoftComparingLanguages/


This commentary is a case of one inexperienced programmer criticizing a
feature that they do not understand, with poor understanding of the
forces that underlie the fundamental reasoning. Garbage collection is
easy in C++... if every developer were free of mistakes and if code were
not complex. This is not the case in the real world. The author of that
article completely failed to recognize the reality of memory leaks in a
production system of substantial size.

One reason for the success of BOTH Java and .Net languages like C# is
that this problem is solved for you. You may not agree with the way in
which it is solved, but it is solved for you. That is a huge step up and
a major boon for software development.

The problem isn't the computers or their languages... it is the
limitations of the humans who use them.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.


Jul 21 '05 #11
Hello Joe,

I appreciate that you are taking a reasoned approach.

I also agree that a case could be made for fine-tuning the GC. That said,
Willy's comment is correct... the GC really does run more often than you
think. Also page swapping, as caused by .Net apps, is probably less than
you think. Note that a page is only swapped "in" when memory on that page
is referenced. If the page is completely empty of active objects, then it
will not swap in until the GC frees it. (This actually slows the system
down if you do this too often).

You refer to how much faster the system runs after a while. This is
completely true. However, it is also often the case that the culprit for
this kind of memory fragmentation is the use of unmanaged objects that don't
drop their references and therefore create gaps in the memory space that is
difficult for the heap allocation system to reuse.

As for low-memory systems, I would assert that you are using the OS at or
near the smallest amount of memory that it supports. Normal OS operations
will fragment the memory, all by themselves, without any help from your app.
In this case, .Net is not able to really improve the situation.

Note that the OS teams have made real strides over the years in improving
how memory is used. However, as memory has become so much less expensive,
it is not unreasonable to expect that users will occasionally upgrade their
memory when they install a new OS to get new features like better security,
better handling of multimedia, more efficient file systems and (strictly for
the non-server environments) more advanced games.

I hope this helps. I understand your frustration. (One of my systems at
home has 256M of RAM, too... I'm just too lazy to run down to Fry's and get
a memory module for it :-(.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"joe" <jo*****@rot.of m.net> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
Nick,
Your reasoning is respected and welcomed. I don't hate or love something
like idiots, i simply point out the weak points.

It is true that CG is a great miracle in C# and java, and help the
programmer to concentrate on more critical aspects of his work, however,
when it comes to page faults, it exhibits its weak points.

You might say that because allocation or garbage collection is done only
when the computer becomes low on memory, you gain much higher performance
in your application. This can be true for small programs, however, when
some real big programs are executed on computers with low memory ( sth
like 256 MB) , then the amount of OS's page swapping and virtual memory
reading and writing really slows down the entire system. Just think, why
your OS is much faster after a clean restart than the time it is run for
long hours of application executing and playing with the virtual memory?

Well, at least there could be stricter algorithm implemented, which would
force the GC to come into play much sooner that it does now on low-memory
PCs.
"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:pI******** ************@co mcast.com...
PS: i wont hurt you to read this:
http://www.cs.tut.fi/~warp/MicrosoftComparingLanguages/


This commentary is a case of one inexperienced programmer criticizing a
feature that they do not understand, with poor understanding of the
forces that underlie the fundamental reasoning. Garbage collection is
easy in C++... if every developer were free of mistakes and if code were
not complex. This is not the case in the real world. The author of that
article completely failed to recognize the reality of memory leaks in a
production system of substantial size.

One reason for the success of BOTH Java and .Net languages like C# is
that this problem is solved for you. You may not agree with the way in
which it is solved, but it is solved for you. That is a huge step up and
a major boon for software development.

The problem isn't the computers or their languages... it is the
limitations of the humans who use them.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.


Jul 21 '05 #12
I really love the GC. :-)
Not because I have a lot of memory leaks in my pogram, but because my
programming gets far simpler because I do no have to keep track of which
list created the object and in what order do I need to release it to prevent
an access violation if it is referenced by another list.

This way I am guaranteed that the object is only released when bot lists are
not referencing it anymore.
In the none-GC way I have to keep track of that creating additional code
that might even be slower because the GC way is far more optimized than the
code that I would create. The closest to what is equivalent in the GC is
what COM objects does. When the reference counter reaches zero it disposes
itself. But you do not have to call the AddRef and release.

The only negative side of the GC is that it sometimes starts to collect and
for a game that means a momentarely freeze which is deadly if you were about
to kill the bad guy online. ;-) Or maybe when you have a program that must
do something real-time like capturing a video that might lose frames.
Jul 22 '05 #13
On Fri, 24 Jun 2005 14:18:13 +0430, joe wrote:
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 on memory. This is very foolish, and what good is a garbage collector
which doesn't collect the disposed objects when they aren't needed anymore?

Besides, calling CG.Collect() is usually avoided for performance and speed.
What else can i do?

PS: i wont hurt you to read this:
http://www.cs.tut.fi/~warp/MicrosoftComparingLanguages/


This might be a futile suggestion, unhelpful for your situation ... but I
have noticed that GC is more agressive when the project is compiled for
release, rather than debug.
Jul 22 '05 #14
Have you verified the "no GC unless low memory" using the GC perf counters? Task Manager doesn't show you what the GC is doing.

And agreed, in Release build the GC's concept of object liveness is much more aggressive

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

On Fri, 24 Jun 2005 14:18:13 +0430, joe wrote:
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 on memory. This is very foolish, and what good is a garbage collector
which doesn't collect the disposed objects when they aren't needed anymore?

Besides, calling CG.Collect() is usually avoided for performance and speed.
What else can i do?

PS: i wont hurt you to read this:
http://www.cs.tut.fi/~warp/MicrosoftComparingLanguages/


This might be a futile suggestion, unhelpful for your situation ... but I
have noticed that GC is more agressive when the project is compiled for
release, rather than debug.

Jul 22 '05 #15
Like most things there are tradeoffs involved. One of the most
difficult parts of developing large software systems, especially among
multiple developers, is defining and maintaining an object life cycle.
You must define who creates the object, and who has the responsibility
for the storage at any time. In some applications it is fairly
straight forward, in other cases it is not.

C# and the garbage collector removes this burden (for the most part)
from the design and implementation process. However it does this at an
expense. First, there is a performance penality versus what can be
done in native C++. This penality can be relativily small and will
only come into play in the most performance critical of applications.
Second, there is a memory foot print issue. There will be a small
penality for garbage collection.

Finally, there is a point of confusion in the original post. Most
memory schemes, whether they be the garbage collection in Java or C#,
or whether it is the basic malloc/free in C, do not return memory to
the operating system. Memory that is free'ed is made available for the
next request in that process, but the process will almost never return
memory to the OS. Therefore, looking at the memory size for the
process will show the process only getting larger as the process runs
and never getting smaller. If you want to really characterize memory
usage, you need to do that within the process, with the cooperation of
the memory system. There are ways to do this in Visual C++ and C# as
well.
Just my $0.02 worth
Butch

Jul 22 '05 #16
On 28 Jun 2005 20:14:39 -0700, bu****@comcast. net wrote:
Finally, there is a point of confusion in the original post. Most
memory schemes, whether they be the garbage collection in Java or C#,
or whether it is the basic malloc/free in C, do not return memory to
the operating system. Memory that is free'ed is made available for the
next request in that process, but the process will almost never return
memory to the OS. Therefore, looking at the memory size for the
process will show the process only getting larger as the process runs
and never getting smaller.
Yes, but you can tell the difference between a lazy GC and an aggressive
GC: the process running the agressive GC will not grab as much memory from
the OS in the first place, because it will recycle memory from free'd
objects, whereas the lazy GC will just grab more memory until the system
runs low.
If you want to really characterize memory
usage, you need to do that within the process, with the cooperation of
the memory system. There are ways to do this in Visual C++ and C# as
well.

Jul 22 '05 #17
The answer is that in most cases, "No.", you can't recycle disposed objects.
You need to make a new instance of one.
"joe" <jo*****@rot.of m.net> wrote in message
news:uS******** ******@tk2msftn gp13.phx.gbl...
To use a methpore "Do you empty your trash bin, everytime you have thrown
in a paper or whatever?


Can you recycle disposed objects in a programming language? If you can,
then
why do you put them in the recycle bin?
just curious.

Jul 22 '05 #18
Why exactly are you trying to reclaim memory when there is still plenty of
memory for you application to run?
"joe" <jo*****@rot.of m.net> wrote in message
news:ub******** ******@tk2msftn gp13.phx.gbl...

"Patrice" <no****@nowhere .com> wrote in message
news:eZ******** *****@TK2MSFTNG P15.phx.gbl...
Have you tried to call Dispose ?


Dispose is not supported on all objects. In my case, calling GC.collect()
does a little help though.
Though the point of a garbage collector is that there is no need to
reclaim
memory if you have no use for it, it's worth to keep in mind that it have
its root in the managed world and that the Dispose or Close methods
should
still allows to reclaim unmanaged memory immediately...


Yes, in a perfect managed world where no one wants to struggle with memory
allocation and release, this could be good suggestion. But in the same
managed world there are times when an object is used only "once", and then
thrown away forever or used after a long time or maybe in next program
launch. Why should its memory be still occupied by the program?
I mean, there should a way to have more control over this kind of memory
management. ( sth like a half-automatic GC, until the day GC becomes
really
smart and intelligence)

i can also use SetProcessWorki ngSetSize(-1,-1) , however it is not usually
recommended.

Jul 22 '05 #19
GC is a big work! if u set some enviroment variable , GC can occured every
time, but is's too slowly...Of course, if u want to find some memory
problems(such as leak) ,this is a good idea.
GC.Collect() is not a good idea, u'd better NOT call this method in your code.

"Brian Gideon" wrote:


joe wrote:

[snip]

PS: i wont hurt you to read this:
http://www.cs.tut.fi/~warp/MicrosoftComparingLanguages/


I disagree. While some aspects of the article may be at least
partially correct, it does more harm than good. For example, the
author completely missed the point of the original claim about reduced
memory leaks. Similarly, I thought the other points were
misinterpreted as well, albiet, to a lesser degree.

Brian

Jul 22 '05 #20

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

Similar topics

28
367
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...
0
9439
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
10237
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
10071
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
10017
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
6690
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
5326
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
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.