new. It is the heap that is more likely to run out of memory. There are several approaches to solving out-of-memory problems in the heap.
When you are finished using a large data structure, make sure there
are no references to it. It's easy to leave a reference to
unused object around. Assigning
to the reference at the root of the data structure may be sufficient.
I wrote a program which processed source programs in three
phases, building a large data structure for the output of each
phase. I carelessly kept references to the data structures
from the first two phases, even tho I no longer needed them.
I was surprised to run out of memory. Simply assigning
to these variables for the unused data structures solved my problem.