Learn something new every day More Info... by email
Reference counting is a computer programming technique used to streamline memory management, allowing the computer to instantly know when it is "safe" to release a particular value or block of values from memory. When programming, economy is crucial, so that programs can be run on the widest possible range of computer hardware. Random Access Memory (RAM) is is an important but finite resource for a computer. Keeping program data stored in RAM is one of the fastest ways to improve the response time of a program, but consuming too much system RAM makes the program a resource hog, an undesirable trait from the end-user's point of view. Reference counting acts to balance this by keeping data in RAM just as long as it is needed by the program.
The technique of reference counting works by keeping track of the number and name of the objects the program is using. Think of computer memory like a child's room filled with toys, and storage space on the hard drive like a chest of toys in the room. Reference counting keeps track of the specific "toys" (objects) the program (child) is playing with at any one time; in other words, the number of objects that are currently on the "floor." As soon as the program stops playing with a specific object, reference counting takes note of this, and ships the object back into storage on the hard drive, rather than allowing it to continue eating up space in the RAM.
When it comes to benefits, the primary advantage of reference counting comes from immediacy. The instant the program finishes with an object, it puts it away, freeing up space for other objects in the program, and for other programs on the computer. From an end-user point of view, this is a very clean approach; the program never takes up more memory than it requires at any specific time.
The downside of this technique is economy. In order for reference counting to operate effectively, it must utilize a perpetual monitoring device to manage the number of objects being used at any one time. This consumes a small portion of memory and CPU capacity, increasing processor overhead from the program. The result is that the decision to use reference counting becomes a balancing act, determining whether the programmer wants to allocate additional stress to the CPU or the RAM.