GPE breaks down after running the query second time

I created a complex query for clustering data. The query consists of a foreach loop inside a while loop.
When i run the query one time, it gives me the output. But when i run the query second time with some parameter changed, the GPE breaks down and then none of the queries work. I have to restart and run the query again, then it work but it breaks down again when ran the second time.

Can you tell me what causes this and how to get rid of this thing?
Is it because of the load on the system?