<P> As with manual memory management, and unlike tracing garbage collection, reference counting guarantees that objects are destroyed as soon as their last reference is destroyed, and usually only accesses memory which is either in CPU caches, in objects to be freed, or directly pointed by those, and thus tends to not have significant negative side effects on CPU cache and virtual memory operation . </P> <P> There are a number of disadvantages to reference counting; this can generally be solved or mitigated by more sophisticated algorithms: </P> <Dl> <Dt> Cycles </Dt> <Dd> If two or more objects refer to each other, they can create a cycle whereby neither will be collected as their mutual references never let their reference counts become zero . Some garbage collection systems using reference counting (like the one in CPython) use specific cycle - detecting algorithms to deal with this issue . Another strategy is to use weak references for the "backpointers" which create cycles . Under reference counting, a weak reference is similar to a weak reference under a tracing garbage collector . It is a special reference object whose existence does not increment the reference count of the referent object . Furthermore, a weak reference is safe in that when the referent object becomes garbage, any weak reference to it lapses, rather than being permitted to remain dangling, meaning that it turns into a predictable value, such as a null reference . </Dd> <Dt> Space overhead (reference count) </Dt> <Dd> Reference counting requires space to be allocated for each object to store its reference count . The count may be stored adjacent to the object's memory or in a side table somewhere else, but in either case, every single reference - counted object requires additional storage for its reference count . Memory space with the size of an unsigned pointer is commonly used for this task, meaning that 32 or 64 bits of reference count storage must be allocated for each object . On some systems, it may be possible to mitigate this overhead by using a tagged pointer to store the reference count in unused areas of the object's memory . Often, an architecture does not actually allow programs to access the full range of memory addresses that could be stored in its native pointer size; certain number of high bits in the address is either ignored or required to be zero . If an object reliably has a pointer at a certain location, the reference count can be stored in the unused bits of the pointer . For example, each object in Objective - C has a pointer to its class at the beginning of its memory; on the ARM64 architecture using iOS 7, 19 unused bits of this class pointer are used to store the object's reference count . </Dd> <Dt> Speed overhead (increment / decrement) </Dt> <Dd> In naive implementations, each assignment of a reference and each reference falling out of scope often require modifications of one or more reference counters . However, in the common case, when a reference is copied from an outer scope variable into an inner scope variable, such that the lifetime of the inner variable is bounded by the lifetime of the outer one, the reference incrementing can be eliminated . The outer variable "owns" the reference . In the programming language C++, this technique is readily implemented and demonstrated with the use of const references . Reference counting in C++ is usually implemented using "smart pointers" whose constructors, destructors and assignment operators manage the references . A smart pointer can be passed by reference to a function, which avoids the need to copy - construct a new smart pointer (which would increase the reference count on entry into the function and decrease it on exit). Instead the function receives a reference to the smart pointer which is produced inexpensively . </Dd> <Dt> Requires atomicity </Dt> <Dd> When used in a multithreaded environment, these modifications (increment and decrement) may need to be atomic operations such as compare - and - swap, at least for any objects which are shared, or potentially shared among multiple threads . Atomic operations are expensive on a multiprocessor, and even more expensive if they have to be emulated with software algorithms . It is possible to avoid this issue by adding per - thread or per - CPU reference counts and only accessing the global reference count when the local reference counts become or are no longer zero (or, alternatively, using a binary tree of reference counts, or even giving up deterministic destruction in exchange for not having a global reference count at all), but this adds significant memory overhead and thus tends to be only useful in special cases (it is used, for example, in the reference counting of Linux kernel modules). </Dd> <Dt> Not real - time </Dt> <Dd> Naive implementations of reference counting do not in general provide real - time behavior, because any pointer assignment can potentially cause a number of objects bounded only by total allocated memory size to be recursively freed while the thread is unable to perform other work . It is possible to avoid this issue by delegating the freeing of objects whose reference count dropped to zero to other threads, at the cost of extra overhead . </Dd> </Dl> <Dd> If two or more objects refer to each other, they can create a cycle whereby neither will be collected as their mutual references never let their reference counts become zero . Some garbage collection systems using reference counting (like the one in CPython) use specific cycle - detecting algorithms to deal with this issue . Another strategy is to use weak references for the "backpointers" which create cycles . Under reference counting, a weak reference is similar to a weak reference under a tracing garbage collector . It is a special reference object whose existence does not increment the reference count of the referent object . Furthermore, a weak reference is safe in that when the referent object becomes garbage, any weak reference to it lapses, rather than being permitted to remain dangling, meaning that it turns into a predictable value, such as a null reference . </Dd>

What is garbage collection in java in hindi