PHPackages                             live627/phpunit-garbage-collector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. live627/phpunit-garbage-collector

ActiveLibrary[Testing &amp; Quality](/categories/testing)

live627/phpunit-garbage-collector
=================================

Reset properties to save memory after each test

1.0.0(3y ago)03MITPHPPHP &gt;=7.4

Since Nov 15Pushed 3y ago1 watchersCompare

[ Source](https://github.com/live627/phpunit-garbage-collector)[ Packagist](https://packagist.org/packages/live627/phpunit-garbage-collector)[ RSS](/packages/live627-phpunit-garbage-collector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

PHPUnit Garbage Collector
=========================

[](#phpunit-garbage-collector)

[![Tests](https://github.com/live627/phpunit-garbage-collector/actions/workflows/php.yml/badge.svg)](https://github.com/live627/phpunit-garbage-collector/actions/workflows/php.yml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5ae4ee33416a53b8b61cbbc559bee4a1cc6fb2644bc836c9c5b4f943cdd1417a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6976653632372f706870756e69742d676172626167652d636f6c6c6563746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/live627/phpunit-garbage-collector/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/30dff3c6ac94a950804d1e1caf1fa2bf24a546247b11cf09bd95a187e9c452ec/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6976653632372f706870756e69742d676172626167652d636f6c6c6563746f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/live627/phpunit-garbage-collector/?branch=master)

PHPUnit seems to waste a lot of memory because it instantiates a lot of objects before actually starting any test, meaning that the initial memory usage varies depending on the number of tests available.

Say, for example, that you have `UserTest` with 1k test methods. This will unfortunately result in 1k UserTest class instances before any of the tests are actually run. This is actually by design. In order to determine the number of tests, PHPUnit runs `@dataProvider` methods before actually running the tests (and the setUp method).

The problem comes once all of the tests in the test case have finished. Objects in memory are closer than they appear because they aren't dereferenced until after the entire test suite has finished. [Fortunately, this is on the radar](https://github.com/sebastianbergmann/phpunit/issues/4705).

Inspired by [Kris Wallsmith faster PHPUnit article](http://kriswallsmith.net/post/18029585104/faster-phpunit). The claim that this test listener that speeds up PHPUnit tests about 20% by freeing memory is dubious and probably is outdated now that [PHP 7 has rewritten `zval`s to be a bit more compact](https://www.npopov.com/2015/06/19/Internal-value-representation-in-PHP-7-part-2.html).

Installation
------------

[](#installation)

To install this library, run the command below and you will get the latest version

```
composer require live627/phpunit-garbage-collector --dev
```

Usage
-----

[](#usage)

Just add to your `phpunit.xml` configuration

```

```

### Ignoring Tests

[](#ignoring-tests)

Sometimes it is necessary to ignore specific tests, where freeing their properties is undesired. For this use case, you have the ability to *extend the behaviour* of the listener by implementing the `IgnoreTestPolicy` interface.

As an example, if we hypothetically wanted to ignore all tests which include "Legacy" in their test filename, we could create a custom ignore policy as follows

```
