server – Why Ubuntu 16.04 is working smoothly with heavy workload while Ubuntu 20.04 is freezing

I am running Ubuntu on two the same instances on AWS with 1 CPU and 1 GB RAM. First instance is running Ubuntu 20.04 and the second is running Ubuntu 16.04

I did installation of apache2 and php7.3 and mysql exactly the same way on both instances.

I have noticed a strange behavior while I was working on simple PHP script that reads a lot of data from XML and keeps it in array and processes it. I have to keep it in array because I need to change values based on some conditions. I have used unset() function for variables that holds large array immediately after it is used and not needed anymore.

On the latest Ubuntu 20.04 LTS, the script works fine for the first and second time, but it freezes on the third time I run it. I have watched resources being used with top command and I noticed Memory is getting full and not being freed.

Interestingly, I tested the same script on Ubuntu 16.04 and it works very smoothly. I have watched resources being used with top command and I noticed Memory is getting freed once script is executed. So the next execution also runs smoothly.

I am running both scripts on the same type of machine: 1CPU, 1GB RAM, 1GB Swap

Swap is not an issue because I have tried it on better machine without swap: 2CPU and 2GB RAM with Ubuntu 20.04 and it freezed too.

This is screenshot from top command on Ubuntu 20.04:
enter image description here

Nothing is being executed now, but swap is full and it does not decrease as I run the script. This happens after I run the script 2-3 times.

This is screenshot from top command on Ubuntu 16.04:
enter image description here

Nothing is being executed now, and memory is always at 500-600MB usage. Even if I run the script over and over again, memory usage comes to 500-600MB by itself. And running the script is never interrupted.

This is not an issue with AWS because I made test on Digital Ocean and the same thing is happening there.

Anybody knows what is wrong with Ubuntu 20.04? Is it a bug in memory management?

I am asking this question because everything is working fine on Ubuntu 16.04. If it was not working fine there, I would say that this is how it works and I would try to optimize my script.
The only difference is operating system. Everything else is the same on both machines.