Would a factory be able to achieve this? Keep a reference to everything it had constructed?
For your own classes/types and factory for every other class that you can in “client” code, yes. But harder or impossible with most languages for builtin types that you don’t instantiate directly.