473,624 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A re-announce on GC's defects

GC is really garbage itself

Reason 1:

There is delay between the wanted destruction and the actual destruction.

Negative effects by the destruction delay:

1) Efficiency issue

It's bad for CPU/Resource intensive but memory cheap objects.

CPU intensive objects refer to objects who own internal threads.

Resource intensive objects refer to objects who own unmanaged resources like
file handle, network connections, etc.

Don't tell me these objects are rare. Everything is possible to happen and a
general purpose language should not find any excuse not to apply to some
situations.

2) Logic issue

The need for weak reference makes the destruction delay logically incorrect.

Weak references (or you can call them handles) refer to references who do
not require the referenced targets to keep alive, while strong references
do.

When all strong references to a target go out of their lifetime, the target
also comes to the end of its lifetime. Right at this point, weak references
to this target become invalid. However, the destruction delay caused by the
GC violates this logic. Weak references will continue to think the target
alive until the GC really collects the target object.

Don't tell me the IDispose pattern is for that. There may be more than one
strong references and you don't know when and where to call Dispose.

Don't tell me WeakReference (C#) is for that. If you don't have Dispose
called properly, WeakReference still gives a wrong logic.

Don't tell me this can be solved by adding method like IsDestroyed to the
target. It holds a candle to the sun and it only adds to the complexity of
logics.

An example:

Suppose we're doing a 3D game. A radar is monitoring a target. Obviously,
the radar should hold a weak reference to the target. When the target is
killed, logical confusion is immediately brought to the radar watcher (the
gamer). Is the target destroyed or not? You can not tell him, hey, it's
killed but still shown on the radar because you've got to wait for the GC to
make it.

Reason 2:

Poor scalability

This is a Theory issue.

GC is global, which means it scales as the application's memory use scales.
Theoretically, this indicates a bad scalability.

Don't tell me an application should not use too many concurrent objects.
Again, everything is possible. Restrictions only prove the defects of the
language.

Fairly speaking, GC itself is not garbage. However, when java and C#
integrate it and prevent the user from manually managing memory, it becomes
garbage. Note GC in java and C# is not really an addictive as someone would
argue since there is no way to do real memory management like delete obj in
C++.

Better memory management in my mind is reference counting + smart pointer,
which makes things automatic and correct. You have deterministic
destructions while no need to manually call Dispose. You need not to
manually change the reference count as smart pointers help you achieve it.
The only problem with this approach is cyclic reference. However, even if
not theoretically proven, the problem generally can be solved by replacing
some strong references with weak references.

I believe the restriction by GC is one of the main reasons why in some field
(the gaming industry, for example), java or C# is rarely used in serious
products who face real computing challenges.

Solution

1) The ideal solution is to convince the language providers to give us back
the ability of managing memory by our own. GC can still be there, and it
becomes a real addictive in that situation.

2) Transfer the burden to the user. We can ask the user to always take
special cautions (for example, always use "using" in C# to have Dispose
correctly called even exception occurs). Things can work around if the user
do them right. However, that's at risk in nature and not a robust solution.
Jan 18 '07 #1
62 2733
Born wrote:
GC is really garbage itself
[snip]
Better memory management in my mind is reference counting + smart pointer,
So stick with C++ ...
which makes things automatic and correct.
.... because after all, C++ is famous for its programs never having any
kind of memory-allocation-related bugs.

Oh wait.
I believe the restriction by GC is one of the main reasons why in some field
(the gaming industry, for example), java or C# is rarely used in serious
products who face real computing challenges.
How nice for you. Any evidence for this belief, or is it blind faith?
--
Larry Lard
la*******@googl email.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jan 18 '07 #2
I think you have actually missed many very important reasons why the GC
is bad. I hate it all the way. But we have very little choice. It is
either we use .Net and there is a GC or we use Win 32 compiled app
written in C++ or Delphi. Nothing obliges us to use .Net.

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/
Jan 18 '07 #3
Hello Born,

BGC is really garbage itself
B>
BReason 1:
B>
BThere is delay between the wanted destruction and the actual
Bdestruction.
B>
BNegative effects by the destruction delay:
B>
B1) Efficiency issue

Does it really bad? :) It only depends on your app context. If it doesnt
meets your requirement welcome back to unmanaged world with manually memmory
management

B2) Logic issue
B>
BThe need for weak reference makes the destruction delay logically
Bincorrect.

Caching is the logically incorrect too?

BThe only problem with this approach is cyclic
Breference. However, even if not theoretically proven, the problem
Bgenerally can be solved by replacing some strong references with weak
Breferences.

But it's logically incorrect, as mentioned before :)

BI believe the restriction by GC is one of the main reasons why in
Bsome field (the gaming industry, for example), java or C# is rarely
Bused in serious products who face real computing challenges.

lol
What does the "serious product" and "real computing challenge" mean for your?

The FPS? :)

BTW, last DX samples are in C#. The real challenge of C# and game industry
is performance.
In 2 - 3 years it will be solved
BSolution
B>
B1) The ideal solution is to convince the language providers to give
Bus back the ability of managing memory by our own. GC can still be
Bthere, and it becomes a real addictive in that situation.

Nobody prohibits u to use C++
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
Jan 18 '07 #4
Born wrote:
GC is really garbage itself
[...]
Negative effects by the destruction delay:
1) Efficiency issue
It's bad for CPU/Resource intensive but memory cheap objects.
It's also bad allocating / deallocating permanently small objects on a
native heap. It's rather time consuming, compared to the managed one and
it doesn't scale that well if you have a multi core CPU.
>[...]
2) Logic issue
[...]
Don't tell me the IDispose pattern is for that. There may be more than one
strong references and you don't know when and where to call Dispose.
You could use reference counting as well for a managed object. Instead
of calling the destructor you call Dispose if the reference counter is 0.
[...]
An example:

Suppose we're doing a 3D game. A radar is monitoring a target. Obviously,
the radar should hold a weak reference to the target. When the target is
killed, logical confusion is immediately brought to the radar watcher (the
gamer). Is the target destroyed or not? You can not tell him, hey, it's
killed but still shown on the radar because you've got to wait for the GC to
make it.
What has the state of an object (resource) to do with the memory it has
allocated ? That's the only thing GC is supposed to do - manage memory.
Reason 2:
[...]
Fairly speaking, GC itself is not garbage. However, when java and C#
integrate it and prevent the user from manually managing memory, it becomes
garbage. Note GC in java and C# is not really an addictive as someone would
argue since there is no way to do real memory management like delete obj in
C++.
In C++ you have many classes which handle the memory by them self, e.g.
most STL classes to ensure that not permanently memory is allocated or
that the memory is consecutive. GC handles this automatically.

Better memory management in my mind is reference counting + smart pointer,
which makes things automatic and correct. You have deterministic
destructions while no need to manually call Dispose.
As I wrote, why not also implementing reference counting for managed
objects, which are calling Dispose if the reference count is 0 ?
Though there is a performance impact, because for thread safe reference
counting you have to use Interlocked functions.
You need not to
manually change the reference count as smart pointers help you achieve it.
Agreed, you would have to call them manually in C#, because there's no
RAII. Which I'm really missing in C#.
The only problem with this approach is cyclic reference. However, even if
not theoretically proven, the problem generally can be solved by replacing
some strong references with weak references.
Yes, but it's sometimes tricky and in complex object hierarchies you
have a very high chance to build cyclic references, which are hard to
deal with.
I believe the restriction by GC is one of the main reasons why in some field
(the gaming industry, for example), java or C# is rarely used in serious
products who face real computing challenges.
Hm, perhaps because one can't see that Java or C# is used. E.g. the game
Chrome is written in Java (not 100% but many parts of it)
Solution
1) The ideal solution is to convince the language providers to give us back
the ability of managing memory by our own. GC can still be there, and it
becomes a real addictive in that situation.
GC doesn't solve resource allocation problems. They are different as in
C++ and so are the problems you have to face. It's the same with memory
handling. In C++ you still have to think over and over again, how the
memory is handled and if it's better to use an object cache. Otherwise
you will face performance problems too.

2) Transfer the burden to the user. We can ask the user to always take
special cautions (for example, always use "using" in C# to have Dispose
correctly called even exception occurs). Things can work around if the user
do them right. However, that's at risk in nature and not a robust solution.
Isn't that the case ? The developer has to use "using" e.g. for file
objects, which shall release the file handles directly after their usage.

I admit that sometimes I'm missing reference counting, when I'm dealing
with objects stored in multiple lists. How shall I know when to call
dispose ?

E.g. if a file object is stored in 2 or more lists and has to be removed
from one of the lists. How do I know if I have to call Dispose ? Only
performant solution for me would be to use reference counting.
Though you can't have smart pointers, which are automatically destroyed
and will decrease the reference count of an object automatically. You
have to do it manually in C#. :-( - perhaps there's a better solution in
C# that I don't know yet ? (any comments and solutions would be highly
appreciated)

Andre

Jan 18 '07 #5
Born wrote:
GC is really garbage itself
Hey, I have a lot of patience, but I'm sorry... when someone starts a
post with a patently moronic statement like that, well, I conclude that
that person is a moron.

GC is worthless garbage? That must be why Java was a flash in the pan.
Hardly anyone uses Java. As one poster said, "Oh wait."

Not that I consider one study a definitive conclusion, but the one
cited here

http://www.eweek.com/article2/0,1895,2065392,00.asp

says,

"Java now holds the market penetration lead at 45 percent, followed by
C/C++ at 40 percent, and C# at 32 percent."

(Yes, I realize that the numbers add up to more than 100%... I assume
that that is because some shops, like ours, use more than one
language.)

I'm not game to draw any solid conclusions from these numbers, but a
general conclusion is fair game: for a memory management method that is
"garbage itself" it's doing very well. I don't think it's out of line
to say that more than half of all new software is being written using a
garbage-collected language.

Given this, I think that there are only two possible conclusions that
you can draw:

1. Half of the programmers in the world are benighted idiots who have
not yet attained the lofty heights of intellectual superiority that you
now enjoy. They would be using C++ if only they would realize The
Truth.

2. You're missing something.

Personally, I vote for door #2.

Jan 18 '07 #6

Born wrote:
Suppose we're doing a 3D game. A radar is monitoring a target. Obviously,
the radar should hold a weak reference to the target. When the target is
killed, logical confusion is immediately brought to the radar watcher (the
gamer). Is the target destroyed or not? You can not tell him, hey, it's
killed but still shown on the radar because you've got to wait for the GC to
make it.
Oh. My. God. You can't seriously tell me that you would use the memory
allocation state of an object to determine whether it is "alive" or not
in a game? Are you really that bad a designer that you would mix the
concepts of a target's relationship to the game and its memory
allocation status? Even when I programmed in C++ I didn't mix ideas
like that: an object is dead when I flag it dead. It may be destructed
immediately, or some time later. What if I decide to keep a list of the
player's kills? Does it then become impossible to kill anything,
because it's in the kill list?

If you're going to trumpet the benefits of deterministic destruction,
please, please don't offer terrible designs as evidence.
I believe the restriction by GC is one of the main reasons why in some field
(the gaming industry, for example), java or C# is rarely used in serious
products who face real computing challenges.
This made me laugh. I love the little caveat, "in some field" You had
to add that because in _many_ fields garbage collected languages do
just fine.

So what is your complaint, really? That a GC language can't handle
every problem in every domain? No shit, Sherlock. There _are_ people
using C# and Java for gaming (the latter mostly for portability to
micro platforms, but that aside...) but I wouldn't use it for that. I
would use C++ to write games for precisely the reasons that you are so
ham-handedly outlining here, which can be summed up in one sentence:
more control. GC is a two-edged sword: it removes a lot of picky
housekeeping that has caused more bugs than anything I know of. (Larry
Lard pointed this out rather sardonically in his post: C++ is infamous
for memory allocation bugs.) The other edge of the sword is that GC
removes control from the programmer, which is what you're complaining
about. For _most_ applications, the loss of control doesn't matter. For
some, such as gaming, it does. So, in those domains, use C++.

I no longer consider C++ a serious language for business development.
You know, the meat-and-potatoes, number-crunching applications that
make up most of the world's software. Why? Because C++ adds a lot of
picky housekeeping (memory management) that gains me nothing. I don't
need the additional control it offers, and the cost of that control is
bugs. For some domains (such as gaming), the tradeoff is warranted. For
most, it's not, which is why Java and C# are doing so well.

If you're so close to the metal that the tiny delays introduced by
..NET's GC will screw up your app, then the answer is simple: DON'T USE
IT. USE C++.

In the end, what are you trying to prove here? That C# can't tackle
every problem under the sun? We all know that. Move on. Nothing to see
here. Or are you trying to demonstrate that C# isn't suited to any
problem domain at all? If so then I refer you to my previous post:
you're missing something.
1) The ideal solution is to convince the language providers to give us back
the ability of managing memory by our own. GC can still be there, and it
becomes a real addictive in that situation.
And this buys us... what? The ability to use C# / Java in a few domains
where it isn't working well right now? Why not just use a better-suited
language? What is it about programmers, all of us, that makes us want
to invent the super-duper maxed-out Swiss Army Knife of programming
languages that can do anything? They already tried that. It was called
Ada. How many people still use Ada? So what if game development is
mostly done using C++ and not C#? Would you do brain surgery with a
Swiss Army Knife? Of course not: you would use specialized tools. What,
then, is so wrong about using the best language suited to a particular
domain, rather than trying to create the language that can do anything?

Jan 18 '07 #7

Born wrote:
GC is really garbage itself

Reason 1:

There is delay between the wanted destruction and the actual destruction.

Negative effects by the destruction delay:

1) Efficiency issue

It's bad for CPU/Resource intensive but memory cheap objects.

CPU intensive objects refer to objects who own internal threads.

Resource intensive objects refer to objects who own unmanaged resources like
file handle, network connections, etc.

Don't tell me these objects are rare. Everything is possible to happen and a
general purpose language should not find any excuse not to apply to some
situations.
They're not rare. In fact, they're all over the place. That's why
IDisposable was created. It provides a mechanism for doing
deterministic finalization. That leaves managed memory as the only
resource whose cleanup is necessarily delayed. But, I think the
decrease in efficiency here is more than offset by the increase in
efficiency during memory allocation.
2) Logic issue

The need for weak reference makes the destruction delay logically incorrect.

Weak references (or you can call them handles) refer to references who do
not require the referenced targets to keep alive, while strong references
do.

When all strong references to a target go out of their lifetime, the target
also comes to the end of its lifetime. Right at this point, weak references
to this target become invalid. However, the destruction delay caused by the
GC violates this logic. Weak references will continue to think the target
alive until the GC really collects the target object.
That's one of the advantages of a WeakReferece though. You can
resurrect a WeakReference target. What GC logic does that violate?
>
Don't tell me the IDispose pattern is for that. There may be more than one
strong references and you don't know when and where to call Dispose.
It depends on whose perspective you're considering. As the developer
of the object you have little control over when or if Dispose gets
called, but as the caller of the object you have absolute control.
Isn't it the callers responsibility to decide when the object isn't
needed any longer? Maybe it's just me, but I'd hate it if the
FileStream decided for me when I was done writing to a file. Now, if
the FileStream reference falls out of scope (and assuming that I don't
have references to it elsewhere) before I've called Dispose then it's
my fault for not using it correctly. Still, the GC is nice enough to
call Dispose for me as a last resort. So, I think I understand your
point. I just don't see it as a big problem.
>

Don't tell me WeakReference (C#) is for that. If you don't have Dispose
called properly, WeakReference still gives a wrong logic.
A WeakReference isn't suppose to be used as a counter-measure to a
missing Dispose call. That's what the class' destructor (or Finalize
method) is used for.
>

Don't tell me this can be solved by adding method like IsDestroyed to the
target. It holds a candle to the sun and it only adds to the complexity of
logics.

An example:

Suppose we're doing a 3D game. A radar is monitoring a target. Obviously,
the radar should hold a weak reference to the target.
I'd hardly call that an obvious conclusion. I would not use a
WeakReference in this situation. In fact, I rarely use a WeakReference
at all.
When the target is
killed, logical confusion is immediately brought to the radar watcher (the
gamer). Is the target destroyed or not? You can not tell him, hey, it's
killed but still shown on the radar because you've got to wait for the GC to
make it.
So don't rely on the GC to propagate that information to the radar.
Code it deterministical ly.
>


Reason 2:

Poor scalability

This is a Theory issue.

GC is global, which means it scales as the application's memory use scales.
Theoretically, this indicates a bad scalability.

Don't tell me an application should not use too many concurrent objects.
Again, everything is possible. Restrictions only prove the defects of the
language.
I disagree with your poor scalability argument. Creating a new object
in .NET is blindingly fast. Do some benchmarks. This speed is
*because* of the GC not in *spite* of it. What might be problematic is
that the GC will suspend all threads in the application while it is
cleaning up memory.
>

Fairly speaking, GC itself is not garbage. However, when java and C#
integrate it and prevent the user from manually managing memory, it becomes
garbage. Note GC in java and C# is not really an addictive as someone would
argue since there is no way to do real memory management like delete obj in
C++.

Better memory management in my mind is reference counting + smart pointer,
which makes things automatic and correct. You have deterministic
destructions while no need to manually call Dispose. You need not to
manually change the reference count as smart pointers help you achieve it.
The only problem with this approach is cyclic reference. However, even if
not theoretically proven, the problem generally can be solved by replacing
some strong references with weak references.

I believe the restriction by GC is one of the main reasons why in some field
(the gaming industry, for example), java or C# is rarely used in serious
products who face real computing challenges.
I think it has more to do with the GC suspending threads at
unpredictable times. That would cause the display to appear jerky.
This may be one scenario where a strategically placed call to
GC.Collect would be appropriate. Regardless, I don't think C# was ever
intended to be a tool for gamer developers.
>


Solution

1) The ideal solution is to convince the language providers to give us back
the ability of managing memory by our own. GC can still be there, and it
becomes a real addictive in that situation.

2) Transfer the burden to the user. We can ask the user to always take
special cautions (for example, always use "using" in C# to have Dispose
correctly called even exception occurs). Things can work around if the user
do them right. However, that's at risk in nature and not a robust solution.
Jan 18 '07 #8

"Andre Kaufmann" <an************ *********@t-online.dewrote in message
news:uw******** ******@TK2MSFTN GP02.phx.gbl...
Born wrote:
>GC is really garbage itself
[...]
Negative effects by the destruction delay:
1) Efficiency issue
It's bad for CPU/Resource intensive but memory cheap objects.

It's also bad allocating / deallocating permanently small objects on a
native heap. It's rather time consuming, compared to the managed one and
it doesn't scale that well if you have a multi core CPU.
The framework GC is actually one of the more efficient garbage collectors
out there. Properly done, reference counting can take up to 50% of your
program's CPU cycles. Reference counting will also leave objects that refer
to each other in memory, even when no other objects refer to them. Google
for my GC posts in late 2005 for sample code in VB 6 and VB 2005 for a
simple program that demonstrates the difference between the mark/sweep
algorithm in dotNet vs the reference counting algorithm in the VB 6/COM
model.

The dotNet GC is actually a three generation GC. What this means is that
when the Gen(0) heap fills up, all accessible objects are marked and their
size is computed. If there is insufficient space in the Gen(1) heap, then
and only then is the Gen(1) heap cleaned up to the Gen(2) heap. Once there
is sufficient space in the Gen(1) heap, the marked objects in the Gen(0)
heap are copied to the Gen(1) heap and the heap pointer in Gen(0) is reset
to the base address. The only time a full mark/sweep/compact garbage
collection is done is when the Gen(2) heap is full. This appears to be done
before the framework requests additional memory from the OS.
[...]
2) Logic issue
[...]
Don't tell me the IDispose pattern is for that. There may be more than
one strong references and you don't know when and where to call Dispose.

You could use reference counting as well for a managed object. Instead of
calling the destructor you call Dispose if the reference counter is 0.

You never have to call the IDispose interface. The GC will do this for you.
Yes, it does result in those objects possibly being left in memory for one
more GC cycle, but the benefit is that you, as the programmer, never need
worry about dangling pointers and memory leaks. As long as your objects
themselves clean up properly in the dispose method, memory leaks and
dangling pointers simply cannot occur. The GC class even provides
interfaces to that if your object allocates a lot of unmanaged system
memory, you can tell the framework how much is being allocated in its
constructor. You then tell the framework about the release of this memory
in your Dispose method.

>
>[...]
An example:

Suppose we're doing a 3D game. A radar is monitoring a target. Obviously,
the radar should hold a weak reference to the target. When the target is
killed, logical confusion is immediately brought to the radar watcher
(the gamer). Is the target destroyed or not? You can not tell him, hey,
it's killed but still shown on the radar because you've got to wait for
the GC to make it.

What has the state of an object (resource) to do with the memory it has
allocated ? That's the only thing GC is supposed to do - manage memory.
You're thinking is backwards. You are letting your resource management
determine the scope of an object's lifetime. You need to use the
applicatoin domain logic, in this case, the object going off the edge of the
radar or being destroyed by a missile to remove the references to the
object. Yes, the object's resources will still be allocated for an
indeterminate time, but the object will never again be able to appear on
your radar anyway. The runtime will eventually get around to releasing
those resources via the object's IDispose interface - you don't have to do
it yourself. In C++ you must explicitely handle the release of the memory
at some point in your program execution. In C#, the runtime will execute
the destruction code for you when it either has idle time or needs the
memory to satisfy a allocation request.
>Reason 2:
[...]
Fairly speaking, GC itself is not garbage. However, when java and C#
integrate it and prevent the user from manually managing memory, it
becomes garbage. Note GC in java and C# is not really an addictive as
someone would argue since there is no way to do real memory management
like delete obj in C++.

In C++ you have many classes which handle the memory by them self, e.g.
most STL classes to ensure that not permanently memory is allocated or
that the memory is consecutive. GC handles this automatically.

>Better memory management in my mind is reference counting + smart
pointer, which makes things automatic and correct. You have deterministic
destructions while no need to manually call Dispose.

As I wrote, why not also implementing reference counting for managed
objects, which are calling Dispose if the reference count is 0 ?
Though there is a performance impact, because for thread safe reference
counting you have to use Interlocked functions.
The performance impact can be huge. Early Smalltalk implementations spent
50% of their time reference counting.
>You need not to manually change the reference count as smart pointers
help you achieve it.

Agreed, you would have to call them manually in C#, because there's no
RAII. Which I'm really missing in C#.
>The only problem with this approach is cyclic reference. However, even if
not theoretically proven, the problem generally can be solved by
replacing some strong references with weak references.
When using weak references, you still need to handle dangling pointer
exceptions.
Yes, but it's sometimes tricky and in complex object hierarchies you have
a very high chance to build cyclic references, which are hard to deal
with.
>I believe the restriction by GC is one of the main reasons why in some
field (the gaming industry, for example), java or C# is rarely used in
serious products who face real computing challenges.

Hm, perhaps because one can't see that Java or C# is used. E.g. the game
Chrome is written in Java (not 100% but many parts of it)
>Solution
>1) The ideal solution is to convince the language providers to give us
back the ability of managing memory by our own. GC can still be there,
and it becomes a real addictive in that situation.
It's there is C# - you can mix C++ modules with C#. The penalty is that you
now spend your time managing memory.
GC doesn't solve resource allocation problems. They are different as in
C++ and so are the problems you have to face. It's the same with memory
handling. In C++ you still have to think over and over again, how the
memory is handled and if it's better to use an object cache. Otherwise you
will face performance problems too.
This is what the IDispose and Finalize model is for. Yes, it takes an
additional GC cycle to free the memory, but the object has a chance to clean
up after itself first.
>
>2) Transfer the burden to the user. We can ask the user to always take
special cautions (for example, always use "using" in C# to have Dispose
correctly called even exception occurs). Things can work around if the
user do them right. However, that's at risk in nature and not a robust
solution.

Isn't that the case ? The developer has to use "using" e.g. for file
objects, which shall release the file handles directly after their usage.

I admit that sometimes I'm missing reference counting, when I'm dealing
with objects stored in multiple lists. How shall I know when to call
dispose ?

E.g. if a file object is stored in 2 or more lists and has to be removed
from one of the lists. How do I know if I have to call Dispose ? Only
performant solution for me would be to use reference counting.
Though you can't have smart pointers, which are automatically destroyed
and will decrease the reference count of an object automatically. You have
to do it manually in C#. :-( - perhaps there's a better solution in C#
that I don't know yet ? (any comments and solutions would be highly
appreciated)
You never need to call Dispose. By telling the compiler that an object
class implements IDisposable, the GC will call Dispose for you before it
actually deallocates memory.
Andre
I have researched various allocation/deallocation schemes over the years.
Here's a quick summary:

Explicit allocation/deallocation
- Example: C malloc/dealloc and C++ new/delete
- Benefits: Very easy to implement in the runtime and inital
allocations/deallocations are fast.
- Drawbacks: Dangling pointers, attempts to access deallocated/deleted
objects (null pointer references), heap fragmentation
- Where I would use: Only in Real-Time environments where reliable response
time is the driving factor.

Reference Counting:
- Example: Early SmallTalk and VB 6
- Benefits: Programmer doesn't have to worry about memory allocation.
Impossible to dangle pointers or attempt to dereference pointers to
deallocated objects.. Easy to implement in the runtime
- Drawbacks: Cyclic objects never being freed from memory. Early SmallTalk
implementations spent up to 50% of their time doing reference counting.
Doesn't handle resources other than memory.
- Where I would use: General purpose computing

Simple Mark/Sweep/Compact
- Example: Some older Lisp implementations
- Benefits: Programmer doesn't have to worry about memory allocation. Self
referencing objects will be collected.
- Drawbacks: Long pauses while the GC cycle runs. I've seen Lisp machines
pause for several minutes while the GC cycle runs. Implementation can be
tricky. Object lifetime cannot be predicted.
- Where I would use: General purpose computing.

Multi-Generational Mark/Sweep/Compact with explicit disposal interfaces
- Example: Java and dotNet
- Benefits: Programmer controls the allocation of unmanaged resources
through constructors and deallocation through destructors, limiting the
amount of code that actually handles this issue. Usually faster than simple
mark/sweep/compact since most GC passes don't need to process all
generations of memory, especially since most objects have very short
accessible lifetimes and won't be accessible the next time the GC needs to
run.
- Drawbacks: Hard to implement (but not too much more difficult than simple
mark/sweep/compact). Object lifetime cannot be predicted. Must provide at
least one more generation than the maximum number of generations the
destruction of an object can take. Otherwise this becomes a very bloated
version of the simple mark/sweep/compaction algorithm.
- Where I would use: Everywhere, including most real-time systems.

As you move up this chain of memory management, you're thinking of how to
manage objects in your program needs to change, especially when moving from
the explicit allocation/deallocation to any of the other three models.
Moving from Reference counting to mark/sweep removes the restriction of not
creating cyclic references, making the end developer's job easier. Moving
from simple mark/sweep/compaction to generational provides, in most cases,
is a major performance boost for the same source code.

Mike Ober.
Jan 18 '07 #9
Born wrote:
There is delay between the wanted destruction and the actual destruction.
The "destructio n" (i.e. disposal) happens under the caller's control.
It's up to the user how much of a delay they want, not GC.

Don't forget that GC manages memory, not resources. If you are trying to
make an argument that GC is bad for managing resources, the simple
answer is that it's not *designed* for managing resources.
1) Efficiency issue
It's bad for CPU/Resource intensive but memory cheap objects.
CPU intensive objects refer to objects who own internal threads.
Resource intensive objects refer to objects who own unmanaged resources like
file handle, network connections, etc.
Here you are, criticising the memory-managing GC for not managing
resources well, the very job it's not designed for.
2) Logic issue

The need for weak reference makes the destruction delay logically incorrect.
Destruction, i.e. disposal of resources other than memory, is the
caller's responsibility, not the GC. GC applies to the collection of
memory, and it's logically correct in the way it manages memory - i.e.
the object lifetime.
Poor scalability
GC is global, which means it scales as the application's memory use scales.
Theoretically, this indicates a bad scalability.
Given enough memory, a copying GC (like the CLR GC) is provably more
efficient than manual memory allocation that treats each allocation
individually. Let me repeat that: it's actually provably *more* scalable
than manual memory allocation.
Note GC in java and C# is not really an addictive as someone would
argue since there is no way to do real memory management like delete obj in
C++.
'delete obj' is a memory safety hole. If there are any other references
to the object being deleted, you've just created a security violation.
That's what GC is there to prevent.
Better memory management in my mind is reference counting + smart pointer,
which makes things automatic and correct.
Reference counting *is* GC - poor GC, because it doesn't deal with
cycles.
You have deterministic
destructions while no need to manually call Dispose.
Here you are confusing GC with resource management again.

-- Barry

--
http://barrkel.blogspot.com/
Jan 18 '07 #10

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

Similar topics

1
4284
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would be better to promote better programming than rely on PHP to compensate for lazy programming?
4
6416
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as well as some color coding, etc. Having trouble finding the same in an editor that'll run on OS X. -- Floydian Slip(tm) - "Broadcasting from the dark side of the moon"
1
4089
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again for adding so much code... <TABLE border="1" bordercolor="#000000" cellspacing="0"> <TR>
11
3997
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
18522
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function blocks until something.exe terminates. Just what I /don't/ want. (Wouldn't an & be nice here! Sigh) I need something.exe to disconnect and run in the background while I
1
3693
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with sockets??
10
4211
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
4409
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an image, which will upload the record to a mySql db so users can then either view all profiles or query.. I.e. show all males in UK, all femails over 35 etc. Now, I'm not asking for How to do this but more what would be the best way? I've looked at...
1
3630
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id NOT IN ( SELECT manufacturers_id FROM products )
3
3472
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with the variable $GET but $GET is not a variable, I thought it came from the page that calls the PHP file? if(($_GET)==""){ this gets an error
0
8246
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
8179
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,...
1
8341
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
8490
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
4084
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
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
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
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
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.