DDC-3088: EntityManager::clear doesn't working with inserting #3832

Open
opened 2026-01-22 14:28:49 +01:00 by admin · 1 comment
Owner

Originally created by @doctrinebot on GitHub (Apr 16, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user ach:

It looks like EntityManager's clear() method doesn't remove objects that was persisted during script execution.

Bellows are two functions. First one insert 10.000 records and use clear after each flush that should remove objects from memory, but instead of that memory usage growths in each iteration. There isn't any other reference for this objects.

I've checked how it works for reading and with clearing it works perfectly - script uses only constant memory.

    private function testInserting($em, $entityClass, $batchSize, $startMemoryUsage)
    {
        for ($i = 1; $i <= 10000; <ins></ins>$i) {

            $item = new $entityClass();
            $item->setName($i);
            $item->setPresentation($i);
            $em->persist($item);

            if ($i % $batchSize == 0) {
                $em->flush();
                $em->clear();

                $currentMemoryUsage = memory*get*usage();
                printf("%d:\n\t%.2f MB (%.2f MB)\n", $i, $currentMemoryUsage /1024 / 1024, ($currentMemoryUsage - $startMemoryUsage) / 1024 / 1024);
            }
        }
    }


private function testReading($em, $entityClass, $batchSize, $startMemoryUsage)
    {
        $q = $em->createQuery("select i from $entityClass i");
        $iterableResult = $q->iterate();

        $i = 0;
        while (($row = $iterableResult->next()) !== false) {
            $em->clear();
            if ($i % $batchSize == 0) {
                $currentMemoryUsage = memory*get*usage();
                printf("%d:\n\t%.2f MB (%.2f MB)\n", $i, $currentMemoryUsage /1024 / 1024, ($currentMemoryUsage - $startMemoryUsage) / 1024 / 1024);
            }

            $i<ins></ins>;
        }
    }
    ``` 
My results:

1) Reading without clearing ($em->clear(); removed)

0:
    22.89 MB (2.63 MB)
1000:
    33.41 MB (13.15 MB)
2000:
    44.04 MB (23.78 MB)
3000:
    53.50 MB (33.24 MB)
4000:
    65.13 MB (44.86 MB)
5000:
    74.81 MB (54.55 MB)
6000:
    84.27 MB (64.01 MB)
7000:
    97.96 MB (77.69 MB)
8000:
    107.40 MB (87.14 MB)
9000:
    117.17 MB (96.91 MB)
10000:
    126.61 MB (106.35 MB)

2) Reading with using clear

0:
    22.89 MB (2.63 MB)
1000:
    26.25 MB (5.99 MB)
2000:
    24.74 MB (4.48 MB)
3000:
    26.72 MB (6.46 MB)
4000:
    24.79 MB (4.52 MB)
5000:
    26.76 MB (6.50 MB)
6000:
    24.81 MB (4.55 MB)
7000:
    26.77 MB (6.51 MB)
8000:
    24.83 MB (4.57 MB)
9000:
    26.81 MB (6.54 MB)
10000:
    24.86 MB (4.60 MB)

3) Inserting without clearing

1000:
    29.50 MB (9.24 MB)
2000:
    37.76 MB (17.50 MB)
3000:
    45.12 MB (24.86 MB)
4000:
    54.34 MB (34.07 MB)
5000:
    61.79 MB (41.53 MB)
6000:
    69.09 MB (48.82 MB)
7000:
    76.40 MB (56.13 MB)
8000:
    83.75 MB (63.48 MB)
9000:
    95.64 MB (75.37 MB)
10000:
    102.98 MB (82.71 MB)

4) Inserting with using clear

1000:
    27.90 MB (7.63 MB)
2000:
    34.64 MB (14.37 MB)
3000:
    40.43 MB (20.17 MB)
4000:
    48.20 MB (27.93 MB)
5000:
    54.12 MB (33.85 MB)
6000:
    59.89 MB (39.63 MB)
7000:
    65.67 MB (45.40 MB)
8000:
    71.43 MB (51.16 MB)
9000:
    81.51 MB (61.25 MB)
10000:
    87.29 MB (67.02 MB)

Originally created by @doctrinebot on GitHub (Apr 16, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user ach: It looks like EntityManager's clear() method doesn't remove objects that was persisted during script execution. Bellows are two functions. First one insert 10.000 records and use clear after each flush that should remove objects from memory, but instead of that memory usage growths in each iteration. There isn't any other reference for this objects. I've checked how it works for reading and with clearing it works perfectly - script uses only constant memory. `````` private function testInserting($em, $entityClass, $batchSize, $startMemoryUsage) { for ($i = 1; $i <= 10000; <ins></ins>$i) { $item = new $entityClass(); $item->setName($i); $item->setPresentation($i); $em->persist($item); if ($i % $batchSize == 0) { $em->flush(); $em->clear(); $currentMemoryUsage = memory*get*usage(); printf("%d:\n\t%.2f MB (%.2f MB)\n", $i, $currentMemoryUsage /1024 / 1024, ($currentMemoryUsage - $startMemoryUsage) / 1024 / 1024); } } } private function testReading($em, $entityClass, $batchSize, $startMemoryUsage) { $q = $em->createQuery("select i from $entityClass i"); $iterableResult = $q->iterate(); $i = 0; while (($row = $iterableResult->next()) !== false) { $em->clear(); if ($i % $batchSize == 0) { $currentMemoryUsage = memory*get*usage(); printf("%d:\n\t%.2f MB (%.2f MB)\n", $i, $currentMemoryUsage /1024 / 1024, ($currentMemoryUsage - $startMemoryUsage) / 1024 / 1024); } $i<ins></ins>; } } ``` My results: 1) Reading without clearing ($em->clear(); removed) 0: 22.89 MB (2.63 MB) 1000: 33.41 MB (13.15 MB) 2000: 44.04 MB (23.78 MB) 3000: 53.50 MB (33.24 MB) 4000: 65.13 MB (44.86 MB) 5000: 74.81 MB (54.55 MB) 6000: 84.27 MB (64.01 MB) 7000: 97.96 MB (77.69 MB) 8000: 107.40 MB (87.14 MB) 9000: 117.17 MB (96.91 MB) 10000: 126.61 MB (106.35 MB) 2) Reading with using clear 0: 22.89 MB (2.63 MB) 1000: 26.25 MB (5.99 MB) 2000: 24.74 MB (4.48 MB) 3000: 26.72 MB (6.46 MB) 4000: 24.79 MB (4.52 MB) 5000: 26.76 MB (6.50 MB) 6000: 24.81 MB (4.55 MB) 7000: 26.77 MB (6.51 MB) 8000: 24.83 MB (4.57 MB) 9000: 26.81 MB (6.54 MB) 10000: 24.86 MB (4.60 MB) 3) Inserting without clearing 1000: 29.50 MB (9.24 MB) 2000: 37.76 MB (17.50 MB) 3000: 45.12 MB (24.86 MB) 4000: 54.34 MB (34.07 MB) 5000: 61.79 MB (41.53 MB) 6000: 69.09 MB (48.82 MB) 7000: 76.40 MB (56.13 MB) 8000: 83.75 MB (63.48 MB) 9000: 95.64 MB (75.37 MB) 10000: 102.98 MB (82.71 MB) 4) Inserting with using clear 1000: 27.90 MB (7.63 MB) 2000: 34.64 MB (14.37 MB) 3000: 40.43 MB (20.17 MB) 4000: 48.20 MB (27.93 MB) 5000: 54.12 MB (33.85 MB) 6000: 59.89 MB (39.63 MB) 7000: 65.67 MB (45.40 MB) 8000: 71.43 MB (51.16 MB) 9000: 81.51 MB (61.25 MB) 10000: 87.29 MB (67.02 MB) ``````
admin added the Bug label 2026-01-22 14:28:49 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 16, 2014):

Comment created by @ocramius:

Would be useful to see what the entity class looks like.

Additionally, the ORM version being affected is also needed.

@doctrinebot commented on GitHub (Apr 16, 2014): Comment created by @ocramius: Would be useful to see what the entity class looks like. Additionally, the ORM version being affected is also needed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3832