views:

52

answers:

2

When reasoning about runtime cost in a garbage collected language, what is the cost of a statement such as myList = null; in terms of 'n' (the number of elements in the list)? For sake of argument, consider the list to be a singly linked list of reference types with no finalisation required.

More generally, I'm looking for any information on how runtime cost can be analysed in a language with GC.

A: 

My own thought is that the cost is likely to be either O(1) or O(n) depending on the collector implementation. In a mark and sweep collector the unreachable objects simply won't be reached, so I could imagine there being no cost associated with clearing them. (Infact there is an ongoing cost simply keeping objects alive, presumably amortised by using generations.) Conversely in a simple reference counting collector I could easily imagine it costing O(n) to do the cleanup...

It's not obvious to me how to reason about this when designing algorithms..

pauldoo
A: 

I suspect you can assume the amortized costs of that statement to be O(1). In practice, that is what I do, and I have never found situation that made me suspect this assumption to be incorrect.

peSHIr
The amortized costs are certainly just O(1), as it would have taken at least O(N) in order to allocate the objects in the first place. So even if the collector takes a one off O(N) to clear them, it has already been "paid for" by the previous O(N) operations required to do the allocations.
pauldoo
What I'm interested in is the worst case cost of that statement, not the amortised cost.
pauldoo
@pauldoo: In the worst case, the garbage collector will decide it's time to perform a full collection and scan over every tracked object, which has cost unbounded by any function of N.
Daniel Stutzbach
@pauldoo: Are you doing real-time and/or some kind of embedded software development? Or are you just curious?
peSHIr
I've recently started learning about data structures that aim for good worst case bounds (ie, don't rely on amortisation). I'm reading a book on the subject, and I'm slightly suspicious that the author doesn't discuss any "hidden" costs due to the GC.In the particular data structure I'm investigating it would be possible to alter the implementation slightly to release only a constant number of objects per operation, so it's slightly moot.
pauldoo