How to test memory limits of a docker container

I’ve been playing around with docker containers recently including setting memory limits. But Google had wasn’t much help. So posting a simple method I discovered here. You do need php installed in your container.

truncate -s 1G /tmp/1G
php -r 'file("/tmp/1G");

All it does is create a 1 gigabyte file in the /tmp directory then attempts to read it in using PHP. PHP being PHP tries to load it all at once and if your memory limits are working correctly should give a Fatal error.