# SystemException[“MemoryAllocationFailure”] bug in Histogram/Bincounts

I'd like to share a pernicious problem with BinCounts and therefore also Histogram. The minimized problem is as follows:

BinCounts[
Join[Range[100], {1000000000000000000}],
{20}
]


SystemException["MemoryAllocationFailure"]


Unfortunately, this is not a standard error, and doesn't provide any further information. Catch[]ing it doesn't really seem to help. What makes it worse is that the error can occur quite sporadically (I was seeing it in automated plotting code). It turns out that the error was happening only when there was an outlier in the data to be sent to Histogram.

I will submit a bug to Wolfram Research. In the meantime, you can circumvent this by actively specifying the range to bin:

BinCounts[
Join[Range[100], {1000000000000000000}],
{0, 100, 20}
]


which will lead, as expected, to:

{19, 20, 20, 20, 20}


This workaround works for Histogram too, of course.