Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Created critical memory overload log.

Petr Patek3795ac008adb
diff --git a/src/autoscaling/snapshotter.js b/src/autoscaling/snapshotter.js
index 9e1f2323b..e62e84701 100644
--- a/src/autoscaling/snapshotter.js
+++ b/src/autoscaling/snapshotter.js
@@ -15,6 +15,7 @@ const DEFAULT_OPTIONS = {
snapshotHistorySecs: 30,
maxClientErrors: 1,
};
+const CRITICAL_OVERLOAD_RATIO = 1.2;
/**
* Creates snapshots of system resources at given intervals and marks the resource
@@ -222,6 +223,7 @@ class Snapshotter {
});
this.memorySnapshots.push(snapshot);
+ this._memoryOverloadWarning(memInfo);
} catch (err) {
log.exception(err, 'Snapshotter: Memory snapshot failed.');
} finally {
@@ -229,6 +231,25 @@ class Snapshotter {
}
}
+ /**
+ * Checks for critical memory overload and logs it to the console.
+ * @ignore
+ * @param {Object} memoryInfo - Memory information
+ */
+ _memoryOverloadWarning(memoryInfo) {
+ try {
+ const criticalOverloadRation = this.maxUsedMemoryRatio * CRITICAL_OVERLOAD_RATIO;
+ const { mainProcessBytes, childProcessesBytes } = memoryInfo;
+ const usedBytes = mainProcessBytes + childProcessesBytes;
+ const isCriticalOverload = usedBytes / this.maxMemoryBytes > criticalOverloadRation;
+ if (isCriticalOverload) {
+ log.warning(`Memory is critical overloaded. Overload ratio: ${usedBytes / this.maxMemoryBytes}`);
+ }
+ } catch (e) {
+ log.exception(e, 'Snapshotter: Memory leak warning failed.');
+ }
+ }
+
/**
* Creates a snapshot of current event loop delay.
* @param {Function} intervalCallback
diff --git a/test/autoscaling/snapshotter.js b/test/autoscaling/snapshotter.js
index 1d6ece9c4..a4297145f 100644
--- a/test/autoscaling/snapshotter.js
+++ b/test/autoscaling/snapshotter.js
@@ -186,6 +186,33 @@ describe('Snapshotter', () => {
delete process.env[ENV_VARS.MEMORY_MBYTES];
});
+ it('correctly logs critical memory overload', () => {
+ const memoryDataOverloaded = {
+ mainProcessBytes: toBytes(3100),
+ childProcessesBytes: toBytes(3000),
+ };
+ const memoryDataNotOverloaded = {
+ mainProcessBytes: toBytes(2500),
+ childProcessesBytes: toBytes(2500),
+ };
+ let logged = false;
+ const warning = () => { logged = true; };
+ const stub = sinon.stub(log, 'warning');
+ stub.callsFake(warning);
+ process.env[ENV_VARS.MEMORY_MBYTES] = '10000';
+ const snapshotter = new Snapshotter({ maxUsedMemoryRatio: 0.5 });
+
+ snapshotter._memoryOverloadWarning(memoryDataOverloaded);
+ expect(logged).to.be.eql(true);
+
+ logged = false;
+
+ snapshotter._memoryOverloadWarning(memoryDataNotOverloaded);
+ expect(logged).to.be.eql(false);
+
+ delete process.env[ENV_VARS.MEMORY_MBYTES];
+ });
+
it('correctly marks clientOverloaded', () => { /* eslint-disable no-underscore-dangle */
const noop = () => {};
// mock client data