I'm getting a strange error on code which previously worked and now dont.


    namespace Itstod\LinkedProducts\Model;

    use Itstod\LinkedProducts\Api\LinkInterface;
    use Itstod\LinkedProducts\Model\ResourceModel\LinkedProducts\Collection;
    use Itstod\LinkedProducts\Model\ResourceModel\LinkedProducts\CollectionFactory;
    use Magento\Catalog\Model\Product;
    use Magento\Catalog\Api\ProductRepositoryInterface;

    class Resthandler implements LinkInterface
     * @var Product
     protected $product;

     * @var LinkedProduct[]
    private $instances = [];

     * @var ProductRepositoryInterface
    private $productRepository;

     * @var CollectionFactory
    private $collectionFactory;

    public function __construct(ProductRepositoryInterface $productRepository,CollectionFactory $collectionFactory)
        $this->productRepository = $productRepository;
        $this->collectionFactory = $collectionFactory;

     * @api
     * @param string $sku product sku.
     * @return LinkedProduct[].
     * @throws \Magento\Framework\Exception\NoSuchEntityException
    public function get($sku) {

         * @var LinkedProduct[]
        $returnvalue = [];

        $this->product = $this->productRepository->get($sku);

        $id = $this->product->getId();

        /** @var Collection $collection**/
        $collection = $this->collectionFactory->create()->addFieldToFilter('head_product_id', $id);

        // $collection = $collection->getSelect();

        /** @var \Itstod\LinkedProducts\Model\LinkedProduct $prod */
        foreach ($collection as $prod)

            $returnvalue[] = $this->productRepository->getById($prod->getData('linked_product_id'));


        return $returnvalue;

this code is giving me:

    "Fatal Error: 'Allowed memory size of 1073741824 bytes exhausted (tried to allocate 803225600 bytes)' in '\/home\/<url>\/public_html\/vendor\/magento\/module-eav\/Model\/Entity\/Collection\/AbstractCollection.php' on line 249"

and as you can i see i commented out most of the code to find this error. The crash is happening in the ->create() function as i have tried it on its own and its still crashing.


Found the error, i was trying to extend the wrong abstract collection, in my collection class.

  • I can see the blank line before the declaration of name space. This may be typo mistakes.. Commented Jun 15, 2018 at 13:00
  • shouldnt be the namespace, the function runs fine without the collectionfactory and returns an empty array. Commented Jun 15, 2018 at 14:13

2 Answers 2


its look like a there is no error in code check once your memory limit and increase it after that still show the same error then you can look into code error .

  • I have increased the memory limit once from 756M to 1024M with no other changes and i still got the error, is this what you mean by memory limit increase? Commented Jun 15, 2018 at 14:10
  • still your facing the same error ?
    – shail
    Commented Jun 15, 2018 at 15:42
  • after increasing from 756 to 1024 i still have the same error yes Commented Jun 18, 2018 at 6:10

You have tried to fetch a large amount of data from your Collection


You should be memmory_limit min 2GB as per as Magento doc 2GB https://devdocs.magento.com/guides/v2.2/install-gde/trouble/php/tshoot_php-set.html

Else, you have to add limit or pager on your collection

Check https://www.fmeextensions.com/blog/how-to-add-magnto-2-pagination-to-custom-collection/

  • Are you sure? because its only two rows with 4 columns in the database, that shouldnt crash it right? Commented Jun 18, 2018 at 6:09
  • And it gets stuck before i even try to use any data as it crashes on ->create() Commented Jun 18, 2018 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.