node-opcua-leak-detector 2.168.0 → 2.169.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-opcua-leak-detector",
3
- "version": "2.168.0",
3
+ "version": "2.169.0",
4
4
  "description": "pure nodejs OPCUA SDK - module leak-detector",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -24,14 +24,14 @@
24
24
  "internet of things"
25
25
  ],
26
26
  "homepage": "http://node-opcua.github.io/",
27
- "gitHead": "653b6d6df801ca17298308089dee32e5b12102b6",
27
+ "gitHead": "82d570d3e95bea689cbbe30096279885c5282245",
28
28
  "files": [
29
29
  "src"
30
30
  ],
31
31
  "dependencies": {
32
32
  "chalk": "4.1.2",
33
33
  "node-opcua-assert": "2.164.0",
34
- "node-opcua-object-registry": "2.168.0",
34
+ "node-opcua-object-registry": "2.169.0",
35
35
  "wtfnode": "^0.10.1"
36
36
  }
37
37
  }
@@ -1,31 +1,31 @@
1
1
  // memoryLeakDetector.js
2
2
  let wtf;
3
3
 
4
-
5
4
  if (process.env.MEM_LEAK_DETECTION_WTF_ENABLED) {
6
- console.log("[LeakDetector] ⚠️ WTFNODE enabled");
7
- wtf = require('wtfnode');
8
- wtf.init();
5
+ console.log("[LeakDetector] ⚠️ WTFNODE enabled");
6
+ wtf = require("wtfnode");
7
+ wtf.init();
9
8
  } else {
10
- console.log("[LeakDetector] ℹ️ WTFNODE disabled. Use MEM_LEAK_DETECTION_WTF_ENABLED=true to enable it.");
9
+ console.log("[LeakDetector] ℹ️ WTFNODE disabled. Use MEM_LEAK_DETECTION_WTF_ENABLED=true to enable it.");
11
10
  }
12
11
 
13
-
14
- let noGCexposed = undefined;
12
+ let noGCexposed;
15
13
 
16
14
  /**
17
15
  * Forces garbage collection if available.
18
16
  */
19
17
  function forceGC() {
20
- if (global.gc) {
21
- global.gc();
22
- noGCexposed = false;
23
- } else {
24
- if (noGCexposed == undefined) {
25
- console.warn('[LeakDetector] ⚠️ Garbage collection not exposed. Run Node.js with --expose-gc flag for more accurate results.');
18
+ if (global.gc) {
19
+ global.gc();
20
+ noGCexposed = false;
21
+ } else {
22
+ if (noGCexposed === undefined) {
23
+ console.warn(
24
+ "[LeakDetector] ⚠️ Garbage collection not exposed. Run Node.js with --expose-gc flag for more accurate results."
25
+ );
26
+ }
27
+ noGCexposed = true;
26
28
  }
27
- noGCexposed = true;
28
- }
29
29
  }
30
30
 
31
31
  /**
@@ -33,11 +33,18 @@ function forceGC() {
33
33
  * @returns {number} Heap used in bytes.
34
34
  */
35
35
  function takeMemorySnapshot() {
36
- if (noGCexposed) {
37
- return;
38
- }
39
- forceGC();
40
- return process.memoryUsage().heapUsed;
36
+ if (noGCexposed) {
37
+ return;
38
+ }
39
+ const { performance } = require("node:perf_hooks");
40
+ const start = performance.now();
41
+ forceGC();
42
+ const end = performance.now();
43
+ const duration = (end - start).toFixed(2);
44
+ if (duration > 1) {
45
+ console.log(`[LeakDetector] ♻️ Garbage collection took ${duration}ms`);
46
+ }
47
+ return process.memoryUsage().heapUsed;
41
48
  }
42
49
 
43
50
  /**
@@ -48,29 +55,27 @@ function takeMemorySnapshot() {
48
55
  * @returns {Object} - Result with before/after memory and leak status.
49
56
  */
50
57
  function checkForMemoryLeak(before, after, threshold = 2) {
58
+ if (noGCexposed) {
59
+ return;
60
+ }
61
+ const heapUsedBefore = before / 1024 / 1024; // MB
62
+ const heapUsedAfter = after / 1024 / 1024; // MB
63
+ const delta = heapUsedAfter - heapUsedBefore;
64
+ const isLeak = delta > threshold;
51
65
 
52
- if (noGCexposed) {
53
- return;
54
- }
55
- const heapUsedBefore = before / 1024 / 1024; // MB
56
- const heapUsedAfter = after / 1024 / 1024; // MB
57
- const delta = heapUsedAfter - heapUsedBefore;
58
- const isLeak = delta > threshold;
59
-
60
- if (isLeak) {
61
- console.warn(
62
- `[LeakDetector] ⚠️ Potential memory leak detected in test: ${delta.toFixed(2)} MB increase (threshold: ${threshold} MB , total : ${heapUsedAfter.toFixed(2)} MB)`
63
- );
64
- } else {
65
- console.log(
66
- `[LeakDetector] ✅ No significant memory leak detected: ${delta.toFixed(2)} MB increase total : ${heapUsedAfter.toFixed(2)} MB`
67
- );
68
- }
69
-
66
+ if (isLeak) {
67
+ console.warn(
68
+ `[LeakDetector] ⚠️ Potential memory leak detected in test: ${delta.toFixed(2)} MB increase (threshold: ${threshold} MB , total : ${heapUsedAfter.toFixed(2)} MB)`
69
+ );
70
+ } else {
71
+ console.log(
72
+ `[LeakDetector] No significant memory leak detected: ${delta.toFixed(2)} MB increase total : ${heapUsedAfter.toFixed(2)} MB`
73
+ );
74
+ }
70
75
 
71
- wtf?.dump({ fullStacks: true }); // Show open handles, listeners, etc.
76
+ wtf?.dump({ fullStacks: true }); // Show open handles, listeners, etc.
72
77
 
73
- return { before: heapUsedBefore, after: heapUsedAfter, delta, isLeak };
78
+ return { before: heapUsedBefore, after: heapUsedAfter, delta, isLeak };
74
79
  }
75
80
 
76
- module.exports = { takeMemorySnapshot, checkForMemoryLeak };
81
+ module.exports = { takeMemorySnapshot, checkForMemoryLeak };