COMPMID-1119 - Bugfix SIGKILL

The problem seems caused by the OpenCL driver that does not release the
allocated memory. In order to solve this problem the OpenCL context is
destroyed every 5000 tests to force the release of the memory

Change-Id: I2135f49d7ff92c7761ec8dba6819db1590e19691
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/131459
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/tests/framework/Framework.cpp b/tests/framework/Framework.cpp
index 81e16c8..fd0afe9 100644
--- a/tests/framework/Framework.cpp
+++ b/tests/framework/Framework.cpp
@@ -24,6 +24,9 @@
 #include "Framework.h"
 
 #include "support/ToolchainSupport.h"
+#ifdef ARM_COMPUTE_CL
+#include "arm_compute/runtime/CL/CLScheduler.h"
+#endif /* ARM_COMPUTE_CL */
 
 #include <chrono>
 #include <iostream>
@@ -517,7 +520,8 @@
 
     const std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now();
 
-    int id = 0;
+    int id          = 0;
+    int id_run_test = 0;
 
     for(auto &test_factory : _test_factories)
     {
@@ -526,7 +530,21 @@
 
         if(_test_filter.is_selected(test_info))
         {
+#ifdef ARM_COMPUTE_CL
+            // Every 5000 tests, reset the OpenCL context to release the allocated memory
+            if((id_run_test % 5000) == 0)
+            {
+                cl::Context::setDefault(cl::Context());
+                CLScheduler::get().set_context(cl::Context());
+                CLKernelLibrary::get().clear_programs_cache();
+
+                cl::Context::setDefault(cl::Context(CL_DEVICE_TYPE_DEFAULT));
+                CLScheduler::get().set_context(cl::Context::getDefault());
+            }
+#endif // ARM_COMPUTE_CL
             run_test(test_info, *test_factory);
+
+            ++id_run_test;
         }
 
         ++id;