Code Monkey home page Code Monkey logo

nuxt-memwatch's Introduction

Quickly watch real-time memory stats of your nuxt app

npm npm (scoped with tag)

Introduction

This module gives you better insights in the heap usages of your nuxt server. Especially when using the node-memwatch peer dependency it can be used to help track down memory leaks. This module uses node-memwatcher and node-memwatch, see their readme's for more information

Allthough other tools may provide the same or better functionality, this module is probably the quickest.

nuxt-memwatch demo

Setup

โ„น๏ธ Please note you dont need to re-build your project when en-/disabling this module, you only need to restart the server

Install
npm install --save nuxt-memwatch
// or
yarn add nuxt-memwatch
Install the node-memwatcher peer dependency (recommended)
npm install --save @airbnb/node-memwatch
// or
yarn add @airbnb/node-memwatch
Add nuxt-memwatch to modules section of nuxt.config.js
  modules: [
    ['nuxt-memwatch', { averages: true }],
  ]

or

  modules: [
    'nuxt-memwatch'
  ],
  memwatch: {
    graph: true,
    graphSetup(setup) {
      setup.metrics.malloc = {
        aggregator: 'avg',
        color: 'cyan'
      }
    },
    graphAddMetric(turtleGraph, stats) {
      turtleGraph.metric('my metrics', 'malloc').push(stats.malloced_memory)
    }
  }

Example

You can run the included example by cloning this repo, run yarn install && yarn build and finally yarn start. Then generate some requests by running ab -c100 -n100000 http://127.0.0.1:3000/, this example uses max ~1.3GB of memory which is fine-tuned for node's default heap size limit of 1.5GB (more specifically, 1.5GB is the default limit of the old space)

Running in development mode

Nuxt is not running (memory) 'optimised' in development mode. Memory leaks are likely so although its' possible to use this module in development mode it is not recommended.

FAQ

Please check the node-memwatcher FAQ

Module Options

Besides the default node-memwatcher options, this module provides some extra options

gcAfterEvery number (0)

If set to a number larger then 0, we will force the gc to run after this number of requests. E.g. when set to 1 the gc runs after every request

๐Ÿ”ฅ This only works when you have either installed the peer dependency or are running node with --expose_gc

nuxtHook string (listen)

Memory leaks and heap usages are probably mostly interesting when the nuxt server is running and serving requests. Therefore we start listening for stats events on the listen hook, unless you are running this module in development mode, then we listen for build:done instead (when you havent change this value). If e.g you would like to debug nuxt generate you could do:

// nuxt.config.js

import { getMemwatch } from 'node-memwatcher'
let memwatch

export default {
  ...
  memwatch: {
    graph: false,
    nuxtHook: 'generate:before'
  },
  hooks: {
    generate: {
      async before() {
        memwatch = await getMemwatch()
      },
      routeCreated() {
        // this probably wont work as you expect
        // as node/v8 will probably be too busy to run the gc
        // but more importantly there is not really a nuxt hook
        // available to do this in the right place
        // This does work however, but the gc call at route `n`
        // can only clear memory usage by previous routes `< n`
        memwatch.gc()
      }
    }
  }
  ...

nuxt-memwatch's People

Contributors

pimlie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nuxt-memwatch's Issues

PM2 can't start service after I use nuxt-memwatch

When I installed nuxt-memwatch and started the service with PM2, PM2 could no longer start the application. It was inexplicable. I saw other people in the forum using memwatch-next. I noticed a hint in the document.

Please note you dont need to re-build your project when en-/disabling this module, you only need to restart the server

What is the meaning of this sentence, why not use it again, because I suspect that my problem is related to this.

How can I use in nuxt3?

Hello, I'm currently use nuxt 3.
I installed nuxt-memwatch and registered in nuxt.config.ts.
But, Cannot start nuxt: Cannot start nuxt: Cannot read properties of undefined (reading 'options') at nuxtMemwatch (~~/node_modules/nuxt-memwatch/lib/module.js:11:12).
I think that the problem is this.options, this.nuxt is undefined, and maybe related with the version of nuxt.
Can you give me any guide for this?

My package.json:

 "devDependencies": {
    "@invictus.codes/nuxt-vuetify": "^0.2.17",
    "@nuxt/devtools": "^0.6.1",
    "@nuxtjs/eslint-config-typescript": "^12.0.0",
    "@types/node": "^18",
    "@typescript-eslint/eslint-plugin": "^5.57.1",
    "@typescript-eslint/parser": "^5.57.1",
    "eslint": "^8.37.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-nuxt": "^4.0.0",
    "eslint-plugin-prettier": "^4.2.1",
    "eslint-plugin-vue": "^9.10.0",
    "nuxt": "^3.5.0",
    "nuxt-gtag": "^0.5.7",
    "prettier": "^2.8.7",
    "sass": "^1.62.0",
    "sass-loader": "^13.2.2",
    "typescript": "^5.0.4",
    "vite-plugin-eslint": "^1.8.1"
  },
  "dependencies": {
    "@ckeditor/ckeditor5-build-classic": "^38.1.1",
    "@ckeditor/ckeditor5-vue": "^5.1.0",
    "@ffmpeg-installer/ffmpeg": "^1.1.0",
    "@nuxtjs/dotenv": "^1.4.1",
    "@pinia/nuxt": "^0.4.9",
    "@sendbird/chat": "^4.7.2",
    "@vuepic/vue-datepicker": "^4.4.0",
    "ckeditor5-custom-build": "file:./ckeditor5",
    "compressorjs": "^1.2.1",
    "date-fns": "^2.29.3",
    "dayjs": "^1.11.7",
    "firebase": "^9.22.1",
    "fluent-ffmpeg": "^2.1.2",
    "image-map": "^2.0.1",
    "js-cookie": "^3.0.5",
    "mitt": "^3.0.0",
    "multer": "^1.4.5-lts.1",
    "nuxt-memwatch": "^0.1.2",
    "nuxt-simple-sitemap": "^2.7.0",
    "nuxt-swiper": "^1.1.0",
    "pinia": "^2.0.35",
    "sharp": "^0.32.1",
    "v3-infinite-loading": "^1.2.2",
    "vue-dompurify-html": "^4.1.4",
    "vuedraggable": "^4.1.0"
  },

My nuxt.config.ts

modules: ["nuxt-swiper", "@pinia/nuxt", "@invictus.codes/nuxt-vuetify", "nuxt-simple-sitemap", "nuxt-gtag", "nuxt-memwatch"],

And the error:

 ERROR  Cannot restart nuxt:  Cannot read properties of undefined (reading 'options')                                                                                                                                    ์˜คํ›„ 2:52:07  

  at nuxtMemwatch (/~~~/node_modules/nuxt-memwatch/lib/module.js:11:12)
  at installModule (/~~~/node_modules/@nuxt/kit/dist/index.mjs:2407:101)
  at async initNuxt (/~~~/node_modules/nuxt/dist/index.mjs:2928:7)
  at async load (/~~~/node_modules/nuxi/dist/chunks/dev.mjs:204:9)
  at async _applyPromised (/~~~/node_modules/nuxi/dist/chunks/dev.mjs:96:10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.