Fix memory leak in test (#18622)
Fix memory leak in test (#18622)

* Fix memory leak in test

test=develop

* Fix memory leak in test
test=develop

* Fix memory leak in test
test=develop

* Pull out vars of the loops
test=develop

chengduoZH/Paddle,chengduoZH/Paddle,tensor-tang/Paddle,PaddlePaddle/Paddle,luotao1/Paddle,tensor-tang/Paddle,chengduoZH/Paddle,luotao1/Paddle,chengduoZH/Paddle,tensor-tang/Paddle,PaddlePaddle/Paddle,PaddlePaddle/Paddle,tensor-tang/Paddle,luotao1/Paddle,chengduoZH/Paddle,luotao1/Paddle,PaddlePaddle/Paddle,luotao1/Paddle,luotao1/Paddle,PaddlePaddle/Paddle,PaddlePaddle/Paddle,PaddlePaddle/Paddle,luotao1/Paddle,tensor-tang/Paddle