The garbage collector looks at all the Objects currently in the program and checks if they are still being referenced in the program.
The Objects currently in the program may include Objects that are not going to be used by the program any more, Objects that the program is currently using, Objects the program has been using and will get back to using, and others.
The way an Object is references is, if at least one other Object, that is currently in the program, needs to use this Object. Brief, and hopefully understandable description.
Now, what the garbage collector (GC) does is, it comes along and checks an Object that is currently in the program. If that Object is being used or will be used, then it is left alone. If the Object is not being used, the GC will then check if any other Object currently in the program is referencing this Object. If no other Object is referencing this Object, then it is deleted from the program, just like the garbage collector in real life comes and collect the garbage when it is no longer needed.
Now, the purpose of it has to do with history. A brief description could start here. In the early days, only the people who built the computer knew how to put programs into the computer. There was no need to worry about collecting the garbage as the memory was not enough and careful deleting and creation of Objects (they were not called Objects back then) was carefully controlled/monitored. But as they were not Objects in the sense that we have them now, it was just "do one instruction after another". Now, no one could save any of the calculations to memory or anything else. It would probably have to be written down and incorporated in another program later.
Since most of these instruction repeated each other, a form of looping was introduced. You could then have one section of code that would be repeated many times, but be located in the one location. This would save on memory and the size of the programs. Basically, you got the "Goto", "Jump", etc instructions.
As things progressed, the need to centralise the data came to the forefront. This includes things like the present day Object.
Now, as programmers could make an Object and store it in memory, little things started to happen. Memory was not returned to the system, either during the execution of the program, when the program shut down, or a combination of the two. This is typically called Memory Leak. To stop the memory leak, and to provide an easy passage for people to come and learn programming, the GC was invented (or something similar). The new GC would manage the memory for the user, and the problems of old would disappear.
2006-09-06 13:39:06
·
answer #1
·
answered by Mark aka jack573 7
·
1⤊
0⤋