question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Size of schema drastically spikes memory (RAM) usage

See original GitHub issue

Bug description

We are seeing high memory usage in our servers and I was able to track it down to including Prisma. Here are the stats:

Bare Nest server: 37.6MB Bare Nest server with simple Schema: 48.7MB Bare Nest server with our production PostgreSQL schema: 307.2MB

Production Schema Stats:

114 Models 32 enums

The memory spike seems to come from the size of our schema. Is this expected? a 250MB spike for running a production sized schema (non-trivial) seems massive.

How to reproduce

Unfortunately I cannot share our production schema (for obvious reasons) ~but this should be reproducible by generating a prisma client for a large schema and including it in a Nest (or express) server.~

A plain Nest app can get generated with npx create-nx-workspace --preset nest and then just following the prisma setup instructions.

~Will gladly provide any additional information to help track it down, or if there is an open source “large” schema anyone knows of I can try and make a repro~

Reproduction:

This doesn’t provide the exact same memory spike as the schema is smaller than ours ~35 models not 114 but it does demonstrate the issue of climbing memory based on schema size:

Repo: https://github.com/yharaskrik/plain-nest-prisma-mem-spike-demo

Steps:

  1. yarn install
  2. yarn prisma generate
  3. yarn nx build
  4. node dist/apps/api/main.js

Startup was about 115MB for me and idle is about 80MB

Without Prisma at all startup is about 35MB and idle is 24MB.

image

Expected behavior

Memory increases but not at such a drastic rate.

Prisma information

// cannot share our production schema, should be reproducible with any large schema, we have 114 models

Pretty standard PrismaClient implementation for Nest

@Injectable()
export abstract class BaseCorePrismaService<T extends Prisma.PrismaClientOptions = Prisma.PrismaClientOptions>
    extends PrismaClient
    implements OnModuleInit, OnModuleDestroy
{
    protected constructor(
        protected readonly config: BaseCorePrismaConfig,
        protected readonly optionsArg?: Prisma.Subset<T, Prisma.PrismaClientOptions>
    ) {
        super(optionsArg);
    }

    async onModuleInit(): Promise<void> {
        await this.$connect();
    }

    async onModuleDestroy(): Promise<void> {
        await this.$disconnect();
    }

    async enableShutdownHooks(app: INestApplication): Promise<void> {
        this.$on('beforeExit', async () => {
            await app.close();
        });
    }
}

Environment & setup

  • OS: Apple M1 Max, 32GB of RAM, Ventura 13.0 (We also see high mem on our deployed alpine linux docker containers)
  • Database: Postgres
  • Node.js version: 14.20.2

Prisma Version

3.15.2

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:4
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
yharaskrikcommented, Nov 9, 2022

@jkomyno heres the results from prisma 4.6.0 and node 18

image

Seems like there is something weird going on. (the 114 model schema is in our repo where as the others are in the repro repo, ill try and use our schema over in the other repo to keep it more consistent)

0reactions
yharaskrikcommented, Dec 7, 2022

Ok so we recently upgrades to Prisma 4.7.1 (latest as of this writing).

103MB startup 88MB idle

This is using a fresh Nest server with nothing else in it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid latency spikes and memory consumption ...
How to avoid latency spikes and memory consumption spikes during snapshotting in an in-memory database · 1. We still need to lock the...
Read more >
Best Practices for Configuring Optimal MySQL Memory ...
If you have spikes of more than 1MB/sec, or constant swap activity, you might need to revisit your memory configuration. MySQL Memory allocation ......
Read more >
RAM usage very high when schema is posted - GraphQL
Currently, when posting a new schema to dgraph, the RAM usage spikes (unpredictable) high which can lead to swapping and unresponsive ...
Read more >
SQL Server slowly hogging RAM, where do i start to ...
The solution is to cap the amount of memory SQL Server can claim by running a script such as the one posted by...
Read more >
How to fix high RAM usage when Windows 11 is idle?
Hi. My Windows PC has 16GB of RAM, and around 60% is in use at all times, even when the computer is idle....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found