Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Deferred produces memory leak? #972

Open
Danbka opened this issue Oct 10, 2021 · 6 comments
Open

Using Deferred produces memory leak? #972

Danbka opened this issue Oct 10, 2021 · 6 comments

Comments

@Danbka
Copy link

Danbka commented Oct 10, 2021

I'm using Deferred to avoid N+1 problem, but with a big amount of entities, it produces out of memory error.

Even without buffer it allocates a lot of memory:

'resolve' => function($blogStory) {
    return new GraphQL\Deferred(function () {
        return true;
    });
}

Is there a way to use it more properly?

@spawnia
Copy link
Collaborator

spawnia commented Oct 11, 2021

Can you do a minimal reproduction of this and do some actual measurements?

@Danbka
Copy link
Author

Danbka commented Oct 11, 2021

Yes, there is a synthetic example without DB and buffers:

$authors = [];
for ($i = 0; $i <= 100; $i++) {
    $authors[$i] = ['name' => 'Name ' . $i];
}

$books = [];
for ($i = 0; $i <= 4000; $i++) {
    $books[$i] = ['title' => 'Title ' . $i, 'authorId' => rand(0, 100)];
}

$authorType = new Type\Definition\ObjectType([
    'name' => 'Author',
    'fields' => [
        'name' => [
            'type' => Type\Definition\Type::string(),
            'resolve' => function ($rootValue, $args) {
                return $rootValue['name'];
            }
        ],
    ],
]);

$bookType = new Type\Definition\ObjectType([
    'name' => 'Book',
    'fields' => [
        'title' => [
            'type' => Type\Definition\Type::string(),
            'resolve' => function ($rootValue, $args) {
                return $rootValue['title'];
            }
        ],
        'author' => [
            'type' => $authorType,
            'resolve' => function ($rootValue, $args) use ($authors) {
//                        return new Deferred(function() use ($authors, $rootValue) {
//                            return $authors[$rootValue['authorId']];
//                        });
                return $authors[$rootValue['authorId']];
            }
        ],
    ],
]);

$queryType = new Type\Definition\ObjectType([
    'name' => 'Query',
    'fields' => [
        'getBooks' => [
            'type' => Type\Definition\Type::listOf($bookType),
            'resolve' => function ($rootValue, $args) use ($books) {
                return $books;
            }
        ],
    ],
]);

$schema = new Type\Schema([
    'query' => $queryType
]);

$data = $request->getParsedBody() ?? [];
$data += ['query' => null, 'variables' => null];

$result = \GraphQL\GraphQL::executeQuery(
    $schema,
    $data['query'],
    null,
    $this->container,
    $data['variables']
);

\TGLog::getInstance(\TGLog::NS_API)->debug((string)(int)(memory_get_peak_usage(true) / 1024 / 1024));

It allocates around 4Mb memory.

But when I start using Deferred (line 35) it allocates around 60Mb memory.

@Danbka
Copy link
Author

Danbka commented Jan 28, 2022

@spawnia Hi there,
I added the minimum reproduction: https://github.com/Danbka/php-graphql-deffred
Can you have a look please?

@laurooyen
Copy link

I'm experiencing similar issues. However when I perform the exact same query there's a random chance of about 50% to get an out of memory error. The other 50% my query executes just fine. Also everything runs fine without Deferred but then the response time is unacceptibly high which is to be expected.

@Danbka
Copy link
Author

Danbka commented Jan 31, 2022

@laurooyen let me know please if you find a solution.

@Danbka
Copy link
Author

Danbka commented Jan 4, 2023

@spawnia Hi there, I added the minimum reproduction: https://github.com/Danbka/php-graphql-deffred Can you have a look please?

Any news on the topic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants