added logic to memory management in spiagent