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.

Question: memory usage

See original GitHub issue

I use the following code to generate a thumbnail image on my parse sever. It does

  • fetch the image from an URL to a buffer
  • run sharp on it to resize, save result in buffer
  • create parse file from buffer
  • save the parse file to DB
Parse.Cloud.httpRequest({url: coverartUrl})
.then(function(response) {
	var sharp = require("sharp");
	return sharp(response.buffer).resize(size, size).toFormat('jpeg').toBuffer()
}).then(function(buffer) {
	console.log("Buffer returned, creating Parse File");
	var base64Buffer = buffer.toString("base64");
	var filename = channel.get("coverart").name() + "_thumbnail.jpg";
	var file = new Parse.File(filename, { base64: base64Buffer }, "image/jpeg");
	return file.save();
}).then(function(file) {
	console.log("Thumbnail saved");
	return channel.set(coverartAttribute, file);
});

I run this code on about 30 images. This will cause nodejs to use > 500MB memory afterwards. Garbage collection does not clean the memory even after waiting some time.

I tried to fix it by explicitly setting some buffers to null. But it did not help. Any idea what I am doing wrong and what is causing the memory leak?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JesusIslamcommented, Apr 29, 2017

@papandreou

I am using v7.8.0 atm. Turned out the culprit was initializing GCS module inside the async function that is called inside a loop. I solved it by initializing only once outside of the loop. After I do that the memory leak problem got away. This makes me unable to ran it using dynamic bucket to upload the picture to, but it works for my current use case, so yea.

Not sharp’s fault I think.

1reaction
papandreoucommented, Apr 29, 2017

@JesusIslam, have you tried upgrading to node.js 7? I’ve found that V8 5.4+ is more eager to return memory to the OS, which made a big difference in a long-running app that uses sharp’s stream API in a way that’s similar to what you’re doing.

Also, if you’re on linux, try adding you app to a cgroup and specify a memory.soft_limit_in_bytes value for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Windows 10 High Memory Usage [Causes and Solutions]
Windows 10 high memory usage is usually related to RAM and virtual memory. Although memory is tightly connected with CPU and hard drive,...
Read more >
Fix High RAM Memory Usage Issue on Windows 11/10 [10 ...
10 Fixes for High (RAM) Memory Usage Issue on Windows 11/10 · 1. Close Unnecessary Running Programs/Applications · 2. Disable Startup Programs ·...
Read more >
How To Fix High RAM/Memory Usage on Windows 10 ...
Key moments. View all · close unnecessary background programs on your pc · close unnecessary background programs on your pc · close unnecessary ......
Read more >
[QUESTION] Memory Usage · Issue #20675 · dotnet/aspnetcore
Memory usage of 'hello world' application. Empty asp.net application with minimal startup and stub controller (Sdk="Microsoft.NET.
Read more >
How do I check memory usage? | Linode Questions
Here are some commands that can be used to check memory usage. free -m. cat /proc/meminfo. vmstat -s. top. htop.
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