1

It appears my express server is juicing all the memory on my sever. I'm only taking a guess at this. I have a process which is running at:

node server.js

This loads a server, receives some data from another server and takes a screen shot. Everything works and it works for a day or so. Then out of nowhere, with no code changes it randomly starts crashing with this error:

internal/child_process.js:366
    throw errnoException(err, 'spawn');
    ^

Error: spawn ENOMEM
    at ChildProcess.spawn (internal/child_process.js:366:11)
    at Object.spawn (child_process.js:551:9)

I originally had 2 gigs of memory on my server. The process would work and much faster it would give me this error. I bumped up the memory to 3 gigs and it lasted a lot longer, over a day and then it started happening again. Running a check of memory in my server.js file I get this:

{ rss: 30277632,
  heapTotal: 20168704,
  heapUsed: 12109848,
  external: 497984 }

From my command line I run: free -m

             total       used       free     shared    buffers     cached
Mem:          3072       2223        848          3          0       1107
-/+ buffers/cache:       1115       1956
Swap:            0          0          0

Someone in another post suggested adding swap memory. Unfortunately on my server provider this doesn't seem possible.

My guess is that it sucks memory until it runs out then I start getting the error. Is there anyway I can clean out the memory cache from express to clean up some space? I'm not too familiar with this. Any ideas?

FabricioG
  • 3,107
  • 6
  • 35
  • 74
  • https://stackoverflow.com/a/32444643/1427161 – doublesharp Jun 14 '19 at 22:20
  • It's possible that you have a memory leak as well, which a swap file will not help with. – doublesharp Jun 14 '19 at 22:21
  • @doublesharp that's the question I was talking about when I said I couldn't add swap memory on mine for some reason. Do you have any suggestion for how to diagnose a memory leak? – FabricioG Jun 14 '19 at 22:22
  • Actually I can do swap memory, let me try that @doublesharp – FabricioG Jun 14 '19 at 22:24
  • Ugh just confirmed I can't add swap space from hosting provider. Any work arounds? @doublesharp – FabricioG Jun 14 '19 at 22:28
  • 2
    You will need to use heap snapshots to study what is taking an increasing amount of memory over time. One classic work-around is to just restart your server every so often (often at something like 4am) so that any memory that has leaked somehow is reclaimed and you start with a fresh server instance every day – jfriend00 Jun 14 '19 at 22:44
  • Great, thank you! @jfriend00 – FabricioG Jun 14 '19 at 22:45
  • It could also be that you are running out of some resource (other than actual memory) that your hosting provider puts limits on in the environment you run in. – jfriend00 Jun 14 '19 at 22:45
  • How often is your code spawning a new process? Are you possibly leaking those processes - not shutting them down so that they accumulate over time, each taking some memory. – jfriend00 Jun 14 '19 at 22:45
  • Every couple of minutes. So I run node server.js for example to get started. Server files up then that server is getting hit sometimes every few seconds. @jfriend00 – FabricioG Jun 14 '19 at 22:51
  • What exactly do you mean by shutting down the process? @jfriend00 – FabricioG Jun 14 '19 at 22:52
  • I'm suggesting (as a work-around) a regular restart of your server to reset its memory usage back to what it is at start. Are you really spawning a new process every few seconds? Pls show us your code that uses `spawn()`. Have you checked the process manager to see how many spawned processes are running? – jfriend00 Jun 14 '19 at 22:56
  • I'm running a package which uses phantoms and some other stuff. I'll have to check how many are running in that. Thank you for all your help your work around I think will work! @jfriend00 – FabricioG Jun 15 '19 at 00:35
  • FYI, I have a small local server (home automation) that is started with `forever` which will automatically restart the server any time it stops. So, I just programmed my server to do an orderly `process.exit()` at 4am every night and after it stops, `forever` just starts it up again automatically. Been working like a charm for 4 years. – jfriend00 Jun 15 '19 at 00:54

0 Answers0