Skip to content
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

Memory not release after shutdown #1058

Closed
tongjun741 opened this issue Jan 25, 2021 · 3 comments · Fixed by #1113
Closed

Memory not release after shutdown #1058

tongjun741 opened this issue Jan 25, 2021 · 3 comments · Fixed by #1113

Comments

@tongjun741
Copy link

tongjun741 commented Jan 25, 2021

log4js version: 6.3.0

const {configure, getLogger, shutdown} = require('log4js');

configure({
  appenders: {
    everything: { type: 'file', filename: 'all-the-logs.log' }
  },
  categories: {
    default: { appenders: [ 'everything' ], level: 'debug' }
  }
});

const logger = getLogger();

let s = [];
for (let i = 0; i < 100; i++) {
  s.push("xxxx");
}
let s1 = s.join(",");

setTimeout(()=>{
  console.log("after setTimeout", process.memoryUsage().rss / Math.pow(1024, 2));
}, 10000)

for (let j = 0; j < 10; j++) {
  for (let i = 0; i < 10000; i++) {
    logger.debug(s1 + i);
  }
  console.log(process.memoryUsage().rss / Math.pow(1024, 2));
}
console.log("before shutdown", process.memoryUsage().rss / Math.pow(1024, 2));
shutdown(()=>{
  console.log("after shutdown", process.memoryUsage().rss / Math.pow(1024, 2));
})

output:

41.60546875
53.203125
64.42578125
71.15234375
86.609375
102.85546875
110.1640625
117.4140625
124.35546875
131.5390625
before shutdown 131.5390625
after shutdown 134.94921875
after setTimeout 134.96875
@lamweili
Copy link
Contributor

lamweili commented Dec 29, 2021

There is no issue with the memory.

It's because the garbage collection of Node.js has yet to take place.
I started with the following codes:

console.log(new Date(), "init", process.memoryUsage().rss / Math.pow(1024, 2));
let numOfTimes = 5;
const interval = setInterval(function() {
  console.log(new Date(), "interval", process.memoryUsage().rss / Math.pow(1024, 2));
  if (!--numOfTimes) clearInterval(interval)
}, 30000);

I ran the original codes with added new Date() to the console.log().

const {configure, getLogger, shutdown} = require('log4js');

configure({
  appenders: {
    everything: { type: 'file', filename: 'all-the-logs.log' }
  },
  categories: {
    default: { appenders: [ 'everything' ], level: 'debug' }
  }
});

const logger = getLogger();

let s = [];
for (let i = 0; i < 100; i++) {
  s.push("xxxx");
}
let s1 = s.join(",");

setTimeout(()=>{
  console.log(new Date(), "after setTimeout", process.memoryUsage().rss / Math.pow(1024, 2));
}, 10000)

for (let j = 0; j < 10; j++) {
  for (let i = 0; i < 10000; i++) {
    logger.debug(s1 + i);
  }
  console.log(new Date(), process.memoryUsage().rss / Math.pow(1024, 2));
}
console.log(new Date(), "before shutdown", process.memoryUsage().rss / Math.pow(1024, 2));
shutdown(()=>{
  console.log(new Date(), "after shutdown", process.memoryUsage().rss / Math.pow(1024, 2));
})

Output shows the decrease once the garbage collection kicks in:

2021-12-29T17:43:17.079Z init 21.44140625
2021-12-29T17:43:17.715Z 42.65625
2021-12-29T17:43:17.773Z 50.26171875
2021-12-29T17:43:17.844Z 66.69140625
2021-12-29T17:43:17.895Z 74.19921875
2021-12-29T17:43:17.952Z 88.765625
2021-12-29T17:43:18.015Z 104.64453125
2021-12-29T17:43:18.065Z 112.39453125
2021-12-29T17:43:18.114Z 120.11328125
2021-12-29T17:43:18.173Z 126.69921875
2021-12-29T17:43:18.230Z 133.53515625
2021-12-29T17:43:18.254Z before shutdown 133.5390625
2021-12-29T17:43:23.451Z after shutdown 174.2578125
2021-12-29T17:43:27.527Z after setTimeout 174.34375
2021-12-29T17:43:47.159Z interval 174.34375
2021-12-29T17:44:17.160Z interval 174.34375
2021-12-29T17:44:47.161Z interval 174.34375
2021-12-29T17:45:17.162Z interval 35.203125
2021-12-29T17:45:47.161Z interval 35.20703125

@gmillerd
Copy link

What happens if you undefine logger after your shutdown? Shutdown doesn't seem to destroy the logger, it just resolves pending actions (eg, commit, cleanup your network connections and sync all you appenders)

@lamweili
Copy link
Contributor

lamweili commented Dec 30, 2021

It is still dependent on when the garbage collection (GC) kicks in, which is normal and not within log4js.

You can try out by changing the following codes (in my previous comment):

let logger = getLogger(); // changed const to let
shutdown(()=>{
  logger = undefined; // newly added
  console.log(new Date(), "after shutdown", process.memoryUsage().rss / Math.pow(1024, 2));
})

Depending on when the GC takes place, the output may or may not be similar to mine (in my previous comment).
Nevertheless, it should be observed that the memory will decrease, after some point in time, once GC kicks in.

Thus, there doesn't seem to be any memory issue based on this issue report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants