Skip to content

JavaScript heap out of memory #12594

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

Closed
3 tasks done
foodiecompany opened this issue Jun 20, 2024 · 24 comments
Closed
3 tasks done

JavaScript heap out of memory #12594

foodiecompany opened this issue Jun 20, 2024 · 24 comments
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK Stale

Comments

@foodiecompany
Copy link

foodiecompany commented Jun 20, 2024

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

7.117.0

Framework Version

@sentry/nextjs

Link to Sentry event

No response

SDK Setup

Sentry.init({
  enabled,

  environment: IS_PRODUCTION ? "production" : "development",

  dsn: "",

  // Adjust this value in production, or use tracesSampler for greater control
  tracesSampleRate: 1,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,

  replaysOnErrorSampleRate: 1.0,

  // This sets the sample rate to be 10%. You may want this to be 100% while
  // in development and sample at a lower rate in production
  replaysSessionSampleRate: 0.1,

  // You can remove this option if you're not planning to use the Sentry Session Replay feature:
  integrations: [
    Sentry.replayIntegration({
      // Additional Replay configuration goes in here, for example:
      maskAllText: true,
      blockAllMedia: true
    })
  ]
});

Steps to Reproduce

npm run build

Expected Result

#26 3.523 staff:build: warn  - In order to be able to deminify errors, @sentry/nextjs creates sourcemaps and uploads them to the Sentry server. Depending on your deployment setup, this means your original code may be visible in browser devtools in production. To prevent this, set hideSourceMaps to true in the sentry options in your next.config.js. To disable this warning without changing sourcemap behavior, set hideSourceMaps to false. (In @sentry/nextjs version 8.0.0 and beyond, this option will default to true.) See https://webpack.js.org/configuration/devtool/ and https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#use-hidden-source-map for more information.
#26 3.523 staff:build: 
#26 172.2 staff:build: 
#26 172.2 staff:build: <--- Last few GCs --->
#26 172.2 staff:build: 
#26 172.2 staff:build: [53:0x7f1f45da40c0]   166470 ms: Mark-sweep 2016.6 (2086.5) -> 1969.1 (2049.4) MB, 1677.0 / 0.0 ms  (average mu = 0.174, current mu = 0.141) allocation failure; scavenge might not succeed
#26 172.2 staff:build: [53:0x7f1f45da40c0]   168145 ms: Mark-sweep 2008.0 (2072.5) -> 1975.8 (2061.5) MB, 1553.9 / 0.0 ms  (average mu = 0.128, current mu = 0.073) allocation failure; scavenge might not succeed
#26 172.2 staff:build: 
#26 172.2 staff:build: 
#26 172.2 staff:build: <--- JS stacktrace --->
#26 172.2 staff:build: 
#26 172.2 staff:build: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Actual Result

image

The build is successful when Sentry is disabled.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 20, 2024
@github-actions github-actions bot added the Package: nextjs Issues related to the Sentry Nextjs SDK label Jun 20, 2024
@AbhiPrasad
Copy link
Member

Hi @foodiecompany could you share your next.config.js file?

The issue here is probably sourcemaps generation (which is handled by nextjs, sentry just is responsible for uploading). What version of next.js/webpack are you using?

@foodiecompany
Copy link
Author

foodiecompany commented Jun 20, 2024

Sure

const { withSentryConfig } = require("@sentry/nextjs");
const path = require("path");

const env = process.env;
const SENTRY_PROJECT_NAME = env.SENTRY_PROJECT_NAME;
const IS_PRODUCTION = env.NODE_ENV === "production";
const STRAPI_MEDIA_HOST = IS_PRODUCTION ? "res.cloudinary.com" : "127.0.0.1";

let config = {
  reactStrictMode: true,
  output: "standalone",
  transpilePackages: [
    "ui",
    "@ant-design",
    "antd",
    "rc-pagination",
    "rc-picker",
    "rc-util"
  ],
  images: {
    remotePatterns: [
      {
        protocol: IS_PRODUCTION ? "https" : "http",
        hostname: STRAPI_MEDIA_HOST
      }
    ]
  },
  experimental: {
    outputFileTracingRoot: path.join(__dirname, "../../"),
    optimizePackageImports: ["lodash", "@ant-design", "antd"],

    // The instrumentation hook is required for Sentry to work on the serverside
    instrumentationHook: true
  }
};

// Injected content via Sentry wizard below
if (SENTRY_PROJECT_NAME) {
  config = withSentryConfig(config, {
    // For all available options, see:
    // https://github.com/getsentry/sentry-webpack-plugin#options

    org: "fastybill-vr",
    project: SENTRY_PROJECT_NAME,

    // Only print logs for uploading source maps in CI
    silent: true,

    // Upload a larger set of source maps for prettier stack traces (increases build time)
    widenClientFileUpload: true,

    // Transpiles SDK to be compatible with IE11 (increases bundle size)
    transpileClientSDK: true,

    // Route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
    // This can increase your server load as well as your hosting bill.
    // Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
    // side errors will fail.
    tunnelRoute: "/monitoring",

    // Hides source maps from generated client bundles
    hideSourceMaps: true,

    // Automatically tree-shake Sentry logger statements to reduce bundle size
    disableLogger: true,

    // Enables automatic instrumentation of Vercel Cron Monitors.
    // See the following for more information:
    // https://docs.sentry.io/product/crons/
    // https://vercel.com/docs/cron-jobs
    automaticVercelMonitors: true
  });
}

module.exports = config;

Dockerfile

FROM node:18-alpine AS base
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev vips-dev git > /dev/null 2>&1

FROM base AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune staff --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/package-lock.json ./package-lock.json
RUN npm install -g node-gyp
RUN npm i sharp
RUN npm install

# Build the project
COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json

# The SENTRY env are send when we deployed the docker image
ARG SENTRY_PROJECT_NAME
ENV SENTRY_PROJECT_NAME=${SENTRY_PROJECT_NAME}

ARG SENTRY_AUTH_TOKEN
ENV SENTRY_AUTH_TOKEN=${SENTRY_AUTH_TOKEN}

ARG SENTRY_DSN
ENV SENTRY_DSN=${SENTRY_DSN}

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN npm run build:staff

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=builder /app/apps/staff/next.config.js .
COPY --from=installer /app/apps/staff/package.json .

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/staff/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/staff/.next/static ./apps/staff/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/staff/public ./apps/staff/public

# Expose ports (for orchestrators and dynamic reverse proxies)
EXPOSE 3001
ENV PORT=3001
ENV HOSTNAME="0.0.0.0"

# Run the nextjs app
CMD ["node", "apps/staff/server.js"]

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 20, 2024
@AbhiPrasad
Copy link
Member

Can you try this workaround? #10468 (comment)

@foodiecompany
Copy link
Author

foodiecompany commented Jun 24, 2024

Can you try this workaround? #10468 (comment)

works thx

{
  // ...
  disableServerWebpackPlugin: true,
  disableClientWebpackPlugin: true
}

@getsantry getsantry bot moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 24, 2024
@leighkendell
Copy link

I'm seeing similar issues in version 8.11.0, specifically when building within a docker container. This version doesn't have the same disableServerWebpackPlugin and disableClientWebpackPlugin options but I've tried with:

unstable_sentryWebpackPluginOptions: {
  disable: true
}

which doesn't seem to help. Builds without the withSentryConfig wrapper work fine.

@lforst
Copy link
Contributor

lforst commented Jun 25, 2024

This is very likely because webpack is consuming a lot of memory generating sourcemaps. You can set the following in version 8.x:

module.exports = withSentryConfig(nextConfig, {
  sourcemaps: {
    disable: true
  }
})

@pascuflow
Copy link

On @sentry/nextjs 8.26.0 , Next.js 14.2.5 , and adding sourcemaps: { disable: true } still getting heap memory error.

    widenClientFileUpload: true,
    sourcemaps: { disable: true },
    hideSourceMaps: true,
    disableLogger: true,
    automaticVercelMonitors: true,

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 15, 2024
@chargome
Copy link
Member

@pascuflow exporting your next.js config without Sentry does not cause any errors? Can you share your next.config.js?

@pascuflow
Copy link

Don't think I can control NODE_OPTIONS on Vercel.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 26, 2024
@lforst
Copy link
Contributor

lforst commented Aug 26, 2024

Pretty sure you can provide the env var in the interface you configure the build script with 🤔

@pascuflow
Copy link

Pretty sure you can provide the env var in the interface you configure the build script with 🤔

You're right, directly in the build script worked.

"build": "NODE_OPTIONS=\"--max-old-space-size=4096\" next build"

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 26, 2024
@chargome
Copy link
Member

@pascuflow did it actually resolve your issue?

@pascuflow
Copy link

@pascuflow did it actually resolve your issue?

Yes, build was successful with repo on Github and Vercel deployment. Logged an error in production and saw it on the Issues tab along with source mapping.

@getsantry
Copy link

getsantry bot commented Nov 23, 2024

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot added the Stale label Nov 23, 2024
@getsantry getsantry bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2024
@mikehuebner
Copy link

I'm still seeing this issue using the Vite plugin, disabling the sourcemaps fixes it but that isn't ideal for anyone using Sentry to help debug things ☹

@chargome
Copy link
Member

chargome commented Jan 7, 2025

@mikehuebner you can try adding NODE_OPTIONS="--max-old-space-size=8192" to your build command

@mikehuebner
Copy link

We have it set to 10gb, so it isn't that. Excluding node modules fixed it!

@royporter7
Copy link

I'm getting this issue even using NODE_OPTIONS="--max-old-space-size=8192" on a 8GB build server. I've also tried playing around with the number to try to get it to work, but nothing does.
I also have sourcemaps disabled.

We have it set to 10gb, so it isn't that. Excluding node modules fixed it!

@mikehuebner Sorry what exactly do you mean excluding node modules?

@mikehuebner
Copy link

mikehuebner commented Jan 28, 2025

I can reference it tomorrow, but there was a comment that shows how to exclude node modules from your source maps!

Edit: if it's still happening with your source maps disabled, I can't help you there 🥲

@royporter7
Copy link

@mikehuebner A reference would be incredibly helpful! A few googles searches + checking related sentry github issues for what you're talking about has left me empty handed. Please share whenever you have the time!

@jdbranham
Copy link

If you have this set to true it might be causing some issues.

widenClientFileUpload: false,

From the docs -

(property) widenClientFileUpload?: boolean
Include Next.js-internal code and code from dependencies when uploading source maps.

Note: Enabling this option can lead to longer build times. Disabling this option will leave you without readable stacktraces for dependencies and Next.js-internal code.

Defaults to false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK Stale
Projects
Archived in project
Development

No branches or pull requests

10 participants