Hope this helps All the Person objects stored in the ArrayList are referenced by the ArrayList itself, so as long as you maintain a reference to the ArrayList, there's an indirect reference to every Person object. The GC won't touch it. If you want the GC to collect these stray Person objects, you can use a WeakReference to a Person in the ArrayList instead of a Person.
Which of these objects are eligible for garbage collection?
This might help you Both Random() instances and the WeakReference are eligible for collection: The first Random was not stored in a local, let alone a local that is later read. The second Random was passed to a WeakReference, so would be OK to collect anyway, but the WeakReference itself is not held anywhere, so that too is eligible for collection.
this one helps. When the code is actually running, none of it will be C# or C++/CLI. All of it will be IL from the C# and C++/CLI and machine code from the native code you're interoperating with. Hence you could re-write part of your question as:
Is Garbage Collection time related to number of objects or size of objects?
like below fixes the issue Object size matters very little for GC performance, whereas object count matters a lot. Therefore you can expect better performance for larger objects, given a fixed memory footprint of those objects.
How many objects are available for garbage collection?
I wish this helpful for you That's incorrect. When you pass an object to a method as a parameter, it passes a reference to that object and nothing more. The reference, cb, to the object becomes null, but c2 is still a reference to that same object, so Java still recognizes that the object on the heap has one reference in the code pointing to it: c2. As Holger pointed out in a comment below, ALL of the objects in this particular snippet are eligible for garbage collection as they are not being used. Java's GC has multiple methods for determining whether an object is eligible to be collected and just the fact that a reference exists that points to the object doesn't shield it from collection, however usage of that object would.