I think it's the matter of the board selection. When I first tried homeuser33's code I used the M5Stack OEM board: M5Stack Arduino>M5Stack-Core2(The PSRAM is enabled). It kept rebooting itself. I then switched to ESP32 Arduino>M5Stack-Core2(The PSRAM is enabled as well), it then worked.
Today, I tried different combinations. It ended up with that the only scenario that it's not working if you use M5Stack Arduino>M5Stack-Core2 with PSRAM enabled. M5Stack Arduino>M5Stack-Core2 with PSRAM disabled works perfectly like world101 said. But if you use ESP32 Arduino>M5Stack-Core2, both enabled and disabled work. So I guess this is the matter of selecting different boards.
Thanks!