Skip to content

Instantly share code, notes, and snippets.

@tarlepp
Last active April 8, 2024 17:03
Show Gist options
  • Save tarlepp/01bd93f379e7bf155bb5194d181d5ce2 to your computer and use it in GitHub Desktop.
Save tarlepp/01bd93f379e7bf155bb5194d181d5ce2 to your computer and use it in GitHub Desktop.
<?php
namespace App\Command;
use App\Entity\DateDimension;
use App\Repository\DateDimensionRepository;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'foo',
)]
class FooCommand extends Command
{
public function __construct(private readonly DateDimensionRepository $repository)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
gc_enable();
$batchSize = 250;
$count = 0;
$entityCount = $this->repository->countAdvanced([]);
for ($offset = 0; $offset < $entityCount; $offset += $batchSize) {
$em = $this->repository->getEntityManager();
$qb = $em->createQueryBuilder();
$query = $qb->select('dd.id')
->from(DateDimension::class, 'dd')
->setFirstResult($offset)
->setMaxResults($batchSize)
->getQuery();
$entities = $query->getResult();
foreach ($entities as $entity) {
//$count++;
}
$entities = null;
//Flush after each batch
$em->flush();
$em->clear();
$this->repository->reset();
gc_collect_cycles();
echo "memory usage $count: " . memory_get_usage() / 1024 / 1024 . PHP_EOL;
}
return Command::SUCCESS;
}
}
dev@php-fpm /app (master)> console foo --no-debug --quiet
memory usage 0: 19.145645141602
[2024-04-08T17:01:54.767001+00:00] deprecation.INFO: User Deprecated: AbstractPlatform::supportsViews() is deprecated. All platforms must implement support for offsets in modify limit clauses. (AbstractPlatform.php:4433 called by EvaluatingGeneratorStrategy.php(54) : eval()'d code:4819, https://github.com/doctrine/dbal/pull/4724, package doctrine/dbal) {"exception":"[object] (ErrorException(code: 0): User Deprecated: AbstractPlatform::supportsViews() is deprecated. All platforms must implement support for offsets in modify limit clauses. (AbstractPlatform.php:4433 called by EvaluatingGeneratorStrategy.php(54) : eval()'d code:4819, https://github.com/doctrine/dbal/pull/4724, package doctrine/dbal) at /app/vendor/doctrine/deprecations/lib/Doctrine/Deprecations/Deprecation.php:210)"} []
memory usage 0: 19.320915222168
memory usage 0: 19.324485778809
memory usage 0: 19.328514099121
memory usage 0: 19.332084655762
memory usage 0: 19.335655212402
memory usage 0: 19.343742370605
memory usage 0: 19.348838806152
memory usage 0: 19.35424041748
memory usage 0: 19.358116149902
memory usage 0: 19.361686706543
memory usage 0: 19.365257263184
memory usage 0: 19.368827819824
memory usage 0: 19.372398376465
memory usage 0: 19.37718963623
memory usage 0: 19.383811950684
memory usage 0: 19.391044616699
memory usage 0: 19.395225524902
memory usage 0: 19.398796081543
memory usage 0: 19.402366638184
memory usage 0: 19.405937194824
memory usage 0: 19.409507751465
memory usage 0: 19.413078308105
memory usage 0: 19.416648864746
memory usage 0: 19.420219421387
memory usage 0: 19.423789978027
memory usage 0: 19.427360534668
memory usage 0: 19.430931091309
memory usage 0: 19.434501647949
memory usage 0: 19.43807220459
memory usage 0: 19.44408416748
memory usage 0: 19.456687927246
memory usage 0: 19.467582702637
memory usage 0: 19.472373962402
memory usage 0: 19.475944519043
memory usage 0: 19.479515075684
memory usage 0: 19.483085632324
memory usage 0: 19.486656188965
memory usage 0: 19.490226745605
memory usage 0: 19.493797302246
memory usage 0: 19.497367858887
memory usage 0: 19.500938415527
memory usage 0: 19.504508972168
memory usage 0: 19.508079528809
memory usage 0: 19.511650085449
memory usage 0: 19.51522064209
memory usage 0: 19.51879119873
memory usage 0: 19.522361755371
memory usage 0: 19.525932312012
memory usage 0: 19.529502868652
memory usage 0: 19.533073425293
memory usage 0: 19.536643981934
memory usage 0: 19.540214538574
memory usage 0: 19.543785095215
memory usage 0: 19.547355651855
memory usage 0: 19.550926208496
memory usage 0: 19.554496765137
memory usage 0: 19.558067321777
memory usage 0: 19.561637878418
memory usage 0: 19.565208435059
memory usage 0: 19.568778991699
memory usage 0: 19.57234954834
memory usage 0: 19.58666229248
memory usage 0: 19.604393005371
memory usage 0: 19.622611999512
memory usage 0: 19.628623962402
memory usage 0: 19.632194519043
memory usage 0: 19.635765075684
memory usage 0: 19.639335632324
memory usage 0: 19.642906188965
memory usage 0: 19.646476745605
memory usage 0: 19.650047302246
memory usage 0: 19.653617858887
memory usage 0: 19.657188415527
memory usage 0: 19.660758972168
memory usage 0: 19.664329528809
memory usage 0: 19.667900085449
memory usage 0: 19.67147064209
memory usage 0: 19.67504119873
memory usage 0: 19.678611755371
memory usage 0: 19.682182312012
memory usage 0: 19.685752868652
memory usage 0: 19.689323425293
memory usage 0: 19.692893981934
memory usage 0: 19.696464538574
memory usage 0: 19.700035095215
memory usage 0: 19.703605651855
memory usage 0: 19.707176208496
memory usage 0: 19.710746765137
memory usage 0: 19.714317321777
memory usage 0: 19.717887878418
memory usage 0: 19.721458435059
memory usage 0: 19.725028991699
memory usage 0: 19.72859954834
memory usage 0: 19.73217010498
memory usage 0: 19.735740661621
memory usage 0: 19.739311218262
memory usage 0: 19.742881774902
memory usage 0: 19.746452331543
memory usage 0: 19.750022888184
memory usage 0: 19.753593444824
memory usage 0: 19.757164001465
memory usage 0: 19.760734558105
memory usage 0: 19.764305114746
memory usage 0: 19.767875671387
memory usage 0: 19.771446228027
memory usage 0: 19.775016784668
memory usage 0: 19.778587341309
memory usage 0: 19.782157897949
memory usage 0: 19.78572845459
memory usage 0: 19.78929901123
memory usage 0: 19.792869567871
memory usage 0: 19.796440124512
memory usage 0: 19.800010681152
dev@php-fpm /app (master)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment