A memory leak occurred when processing some special PDF files.
node.js : v20.17.0
@opendocsg/pdf2md : v0.2.1
unpdf : 1.0.6
A memory leak occurred when I processed the following PDF file using the code below. I used @opendocsg/pdf2md, which indirectly calls unpdf
`const fs = require('fs');
const pdf2md = require('@opendocsg/pdf2md');
const { PDFDocument } = require('pdf-lib');
async function splitPdfToBuffers(path, pagesPerChunk = 10) {
const full = await PDFDocument.load(fs.readFileSync(path));
const total = full.getPageCount();
const buffers = [];
for (let i = 0; i < total; i += pagesPerChunk) {
const sub = await PDFDocument.create();
const indexes = [...Array(pagesPerChunk).keys()]
.map(k => i + k)
.filter(p => p < total);
const copied = await sub.copyPages(full, indexes);
copied.forEach(p => sub.addPage(p));
buffers.push(await sub.save()); // Uint8Array – 足够“完整 PDF”
}
return buffers;
}
// 辅助函数:打印当前内存使用情况
function logMemory(label) {
const mem = process.memoryUsage();
console.log(`🧠 [${label}]`);
console.log(` RSS: ${(mem.rss / 1024 / 1024).toFixed(2)} MB`);
console.log(` HeapTotal: ${(mem.heapTotal / 1024 / 1024).toFixed(2)} MB`);
console.log(` HeapUsed: ${(mem.heapUsed / 1024 / 1024).toFixed(2)} MB`);
console.log(` External: ${(mem.external / 1024 / 1024).toFixed(2)} MB`);
console.log('------------------------------------------');
}
async function convertPdfToMarkdown(filePath) {
try {
logMemory("Start");
const chunks = await splitPdfToBuffers(filePath, 1); // 10 页一块
logMemory("After reading PDF into buffer");
let md = '';
for (let i = 0; i < chunks.length; i++) {
const partMd = await pdf2md(Buffer.from(chunks[i]));
md += partMd + '\n';
logMemory(`after chunk ${i + 1}/${chunks.length}`);
}
logMemory("After converting PDF to Markdown");
return md;
} catch (err) {
console.error("❌ 转换失败:", err);
return null;
}
}
(async () => {
const filePath = 'C:/Users/zhou/Downloads/test-2.pdf';
const markdown = await convertPdfToMarkdown(filePath);
logMemory("After convertPdfToMarkdown() finished");
if (markdown) {
console.log("✅ 转换成功,Markdown 内容前 500 字符:");
console.log(markdown.length);
}
})();
`
[test-2.pdf](https://github.com/user-attachments/files/20897281/test-2.pdf)

关闭于 2025-09-01 1 条评论