Generally a buffer is used for this sort of operation, often a FIFO (First In, First Out) data structure where when new information comes in that would be beyond the buffer capacity, the oldest (first) info in the buffer is purged out to make room for the new data.
This is sort of like reading a file line by line, outputting the results wherever you want them, and then reading the next line. The same goal is sought - to avoid crashing your script due to excess memory usage.
So, what happens when you have a dataset that is just too big to keep in memory? In short - you can't continue. You either need to stop the work and return something like "result set too large - here are first n occurrences", or you need to output the results to a file or some such that lets you get things out of RAM so you can keep working.
A canonical example has become SAX vs DOM in XML processing. When data sets are large, the downside is that you lose the simplicity and convenience of being able to just shove everything into RAM and not worry about it. It almost always requires a shift in the strategy and coding techniques to handle such problems.
"He who increaseth in raw data, increaseth in coding complexity." - computerized Proverb