Upgrade to Pro — share decks privately, control downloads, hide ads and more …

怖くないメモリ肥大化

 怖くないメモリ肥大化

Nihonbashi.js #9のLTで発表しました。
Node.jsでのメモリ肥大化にどう対応するかというお話です。

Tsukasa OISHI

October 31, 2024
Tweet

More Decks by Tsukasa OISHI

Other Decks in Programming

Transcript

  1. ϧʔϓॲཧͰେྔͳΦϒδΣΫτΛੜ੒ function generateLargeNumberOfObjects() { const objects: { id: number; data:

    string }[] = []; for (let i = 0; i < 1000000; i++) { const obj = { id: i, data: ‘aaa….’ }; objects.push(obj); } return objects; }
  2. ΫϩʔδϟʔͰ૿͑ΔσʔλΛอ࣋ function createDataAccumulator() { let data: number[] = []; return

    function addData(newData: number): void { data.push(newData); }; } Const accumulateData = createDataAccumulator(); accumulateData(1); accumulateData(2); …
  3. ($

  4. const chatPrompt = ChatPromptTemplate.fromMessages([ [ "system", "You are a helpful

    assistant that translates {input_language} to {output_language}.", ], ["human", "{text}"], ["human", "{text}"], ["human", "{text}"], ["human", "{text}"], ["human", "{text}"], ["human", "{text}"], ["human", "{text}"], ["human", “{text}"], … ["human", “{text}"], ]);
  5. memoryCheck() { const heap = process.memoryUsage(); const msg = [];

    for (const key in heap) { msg.push(`${key}: ${Math.round(heap[key] / 1024 / 1024)} MB`); } console.log(msg.join(', ')); }
  6. start rss: 164 MB, heapTotal: 31 MB, heapUsed: 28 MB

    end rss: 1137 MB, heapTotal: 999 MB, heapUsed: 959 MB
  7. export async function getEncoding( encoding: TiktokenEncoding, options?: { signal?: AbortSignal;

    extendedSpecialTokens?: Record<string, number>; } ) { (লུ) return new Tiktoken( await cache[encoding], options?.extendedSpecialTokens ); }
  8. start rss: 110 MB, heapTotal: 58 MB, heapUsed: 34 MB

    end rss: 231 MB, heapTotal: 116 MB, heapUsed: 93 MB