Improve GC debug log message code

This pull request is for code review.

The commits fix bug #13998 and also simplifies some code to reduce the chance of getting buffer overruns. The one downside is that the output is no longer formatted with commas.

(pair programmed with @tonyg)