grpc-node icon indicating copy to clipboard operation
grpc-node copied to clipboard

grpc-js version 1.12.2, there is a phenomenon of memory growth after use

Open moumou9215 opened this issue 1 year ago • 3 comments

Problem description

Update to [email protected] Version, experiencing continuous memory growth issue

Reproduction steps

nodejs: const grpc = require('@grpc/grpc-js'); const protoLoader = require('@grpc/proto-loader');

const packageDefinition = protoLoader.loadSync( proto文件路径, { keepCase: true, longs: String, enums: String, defaults: true, oneofs: true, }, ); const dataService = grpc.loadPackageDefinition(packageDefinition);

// The testProto() access is called in real-time and requests every 3 seconds export const testProto= () => { const grpcOptions = {}; const client = new dataService.DataMessage(URL, grpc.credentials.createInsecure()); return new Promise((resolve, reject) => { client.getInfo(grpcOptions, (err, output) => { if (err) { reject(err); } else { resolve(output); } }); }); };

moumou9215 avatar Dec 12 '24 03:12 moumou9215

In your testProto function, you create a new client object, but you never close that client. Because of that, it continues to retain memory after you stop using it. We recommend creating a single client and using it for every request, but if that is not an option, you should at least call client.close() when you are done with it.

murgatroid99 avatar Dec 12 '24 16:12 murgatroid99

Create a client in Node.js and use it for each request (with real-time refresh), and whether there is a need to close or perform other operations afterwards. Which of the following two methods is better in terms of performance

  1. Create a client for each request
  2. Call client. close() after each request is completed

moumou9215 avatar Dec 13 '24 02:12 moumou9215

Your best option is to create a single client object, and use that single client to make every request. This performs better because it ensures that resources such as connections are reused as much as possible.

Your other option is to create a client for each request and call client.close() after the request completes.

murgatroid99 avatar Dec 13 '24 16:12 murgatroid99