Question: I'm doing a polling method to my API every 5 seconds, to get real-time data. The code below works but after 1 hour of running, the page crash(Aww Snap, with the dinosaur image and error: out of memory). The data I'm collecting is quite large, and I'm expecting that javascript will offload the memory(garbage collection) every time the function is being called again. I can see in the Chrome Task Manager, the memory footprint is growing over time. Is there a way to clear the memory or offload the memory from growing over time?

```
 data(){
        return{

            newdata:[],

        };

    },
methods: {
loadData:async function () {

              try {
                      let response = await axios.get('/monitoring_data');
                      if (response.status != 200) {
                        await new Promise(resolve => setTimeout(resolve, 1000));
                        await this.loadData();
                      }else {

                        // Get the data
                        this.newdata= response.data.Ppahvc;

                        // Call loadData() again to get the next data
                         await new Promise(resolve => setTimeout(resolve, 5000));
                         await this.loadData();

                      }

                  } catch (e) {

                      await this.loadData();
                  }
                },
            },
mounted:function(){                     
                    this.loadData();
                  },

```

Answer: 
Use a `JOIN` in your query:

```sql
SELECT * 
FROM movies_table m 
    INNER JOIN directors_table d ON d.director_id = m.dir_id

```

And build the array structure in your loop:

```php
while($r = mysqli_fetch_assoc($sth)) {
    $rows[] = [
        'id' => $r['id'],
        'image' => $r['image'],
        /* other movie keys you need */
        'director' => [
            'id' => $r['director_id'],
            /* other director keys you need */
        ]
    ];
}

```
Two solutions

1. Make a JOIN like @AymDev suggested in the first comment to your question. This might be the preferred solution if your tables are relatively small and you don't have any performance issues
2. Double query

```
    // First retrieve all the directors and keep an array with their info. The Key of the array is the director ID
    $dataQuery = "SELECT * FROM directors_table";
    $sth = mysqli_query($conn, $dataQuery);
    $directors = array();
    while($r = mysqli_fetch_assoc($sth)) {
        $directors[$r['id']] = $r;
    }

    $dataQuery = "SELECT * FROM movies_table";
    $sth = mysqli_query($conn, $dataQuery);
    $rows = array();

    while($r = mysqli_fetch_assoc($sth)) {
        // Retrieve the director info from the previous array
        $r['director'] = $directors[$r['dir_id']];
        $rows[] = $r;
    }

    $respObj->status = 'success';
    $respObj->movies = $rows;

    $respJSON = json_encode($respObj);

    print $respJSON;

```