high_memoryQuery or request caused high memory usage
Elevated memory consumption — potentially large result sets, inefficient data structures, loading entire documents when only a few fields are needed.
Common Causes
- —Fetching entire documents when few fields are needed
- —Large result sets without streaming
- —In-memory joins of large datasets
- —Missing projections
How to Fix
- 1.Use field projections (SELECT field1, field2)
- 2.Implement streaming for large datasets
- 3.Add pagination for large result sets
- 4.Use .lean() in Mongoose for read-only results
Example
typescript
// BAD — loads entire documents including large blobs
const users = await User.find({ active: true });
// GOOD — project only needed fields
const users = await User.find({ active: true }).select('name email createdAt').lean();
// GOOD — stream large result sets
const cursor = User.find({ active: true }).cursor();
for await (const user of cursor) {
await processUser(user);
}.lean() cuts Mongoose memory in half
Mongoose documents carry schema methods, virtuals, and change tracking. For read-only operations,
.lean() returns plain JS objects — typically 3–5× less memory.Prisma streaming large result sets
typescript
// BAD — loads all records into memory at once
const allLogs = await prisma.log.findMany({ where: { level: 'error' } });
// GOOD — process in chunks
const PAGE_SIZE = 500;
let cursor: string | undefined;
while (true) {
const batch = await prisma.log.findMany({
where: { level: 'error' },
take: PAGE_SIZE,
...(cursor ? { skip: 1, cursor: { id: cursor } } : {}),
orderBy: { id: 'asc' },
});
if (batch.length === 0) break;
await processBatch(batch);
cursor = batch[batch.length - 1].id;
}