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

downloadFile files allocate huge memories #1745

Open
ueokande opened this issue Sep 28, 2022 · 0 comments
Open

downloadFile files allocate huge memories #1745

ueokande opened this issue Sep 28, 2022 · 0 comments
Labels
feature New feature pkg: rest-api-client @kintone/rest-api-client

Comments

@ueokande
Copy link
Member

Package

  • @kintone/rest-api-client 3.1.11

Why

The FileClinet downloads blobs into memory. This occurs high memory allocation depends on file size.

I created kintone records which have 3 attachments, each attachment have 1GiB file size. The following code allocate over 6GiB memory size.

// index.js
const { KintoneRestAPIClient } = require("@kintone/rest-api-client");

const client = new KintoneRestAPIClient({
  baseUrl: "https://my-domain.kintone.com",
  auth: {
    username: process.env.KINTONE_USERNAME,
    password: process.env.KINTONE_PASSWORD,
  },
});

(async () => {
  const { record } = await client.record.getRecord({ app: 1, id: 2 });
  const blobs = await Promise.all(
    record["Attachment"].value.map(value => client.file.downloadFile({
      fileKey: value.fileKey,
    }))
  )
  for (const blob of blobs) {
    console.log("Downloaded blob: ", blob.length);
  }

  for (const [key, value] of Object.entries(process.memoryUsage())) {
    console.log(`${key}: ${Math.round(value / 1024 / 1024 * 100) / 100} MiB`)
  }
})();
$ node index.js
Downloaded blob:  1073741824
Downloaded blob:  1073741824
Downloaded blob:  1073741824
rss: 6628.86 MiB
heapTotal: 287.4 MiB
heapUsed: 203.38 MiB
external: 5145.33 MiB
arrayBuffers: 5124.03 MiB

What

Well-known libraries and APIs returns a stream rather than buffer array. fetch() API returns a ReadableStream. It is useful to pipe other process. Of course using streams enable to download into memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature pkg: rest-api-client @kintone/rest-api-client
Projects
None yet
Development

No branches or pull requests

1 participant