Clear memory being used by PHP

后端 未结 4 637
清歌不尽
清歌不尽 2021-02-05 21:54

I\'m running into an interesting problem. I\'m using PHPUnit, and my tests take more memory every single time I run them. Ie...

2.25 MB

2.5 MB

3.0 MB

4条回答
  •  粉色の甜心
    2021-02-05 22:29

    If you are using PDO (or similar database abstraction), then you can use "sqlite::memory:" as you DSN. This has three great advantages:

    1. Automatic clear-up after each test
    2. No chance of accidentally touching a production DB (e.g. even when you run your unit tests on your production server).
    3. All in memory, so tests may run quicker

    The disadvantage is that your SQL has to be portable between MySQL and SQLite. This might turn out to be quite a lot of work (For bigger projects it is often worth it, not just for tests, but for the way it will improve your code's design.) In your case ou mention using Doctrine, in the chat transcript, so you may already have a good abstraction, and therefore it may just run with no changes on SQLite.

提交回复
热议问题