Citrix System Inc Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
If you are using Java as the coding language then one thing you can do, is to make sure that the objects after use should be made to point to null. The garbage collector make sure that the object which are null , the memory related to them on the heap will be free. So the best way to make a garbage collector is to make the object null after the use.
If you are using Java as the coding language then one thing you can do, is to make sure that the objects after use should be made to point to null. The garbage collector make sure that the object which are null , the memory related to them on the heap will be free. So the best way to make a garbage collector is to make the object null after the use.
Basically, there are two way to do automatic garbage collection. First of all, only objects in the heap require garbage collection. Objects in stack are automatically cleaned once the function/program goes out of scope, objs in heap are not cleaned automatically.
- puneet.sohi December 03, 20151. Use reference counting i.e whenever a object is created, we also keep a count of number of pointers referring to that object called the reference count. Then, if the reference count become zero i.e. we have no pointer pointing to that object, we can safely release the memory. A good example of this is the shared/unique/weak pointers in C++ STL
2. We periodically do garbage collection i.e. after some interval a garbage collector runs and any unreferenced memory objects are freed. A good example is Java. Basically, we start from a set of base objects (say all objects in the stack), follow the references and mark any object we encounter on the way as ok. Then we sweep the entire heap again and any object found not marked is unreachable and therefore can be freed.