kovacsv / occt-import-js

The emscripten interface for OpenCascade import functionalities.
GNU Lesser General Public License v2.1
171 stars 24 forks source link

GC not working #34

Open sookie928 opened 1 year ago

sookie928 commented 1 year ago

Hello! I'm currently running this library in a Node.js environment. However, when I execute the ReadStepFile function as per the example provided, it works fine, but the memory used while running the function does not get collected by the garbage collector and continues to accumulate. Is there a way to resolve this issue? Below is the code I've written

const occtimportjs = require('occt-import-js')();
import fetch from 'node-fetch';

export const loadGeometry = async (url: string): Promise<any> => {
  let occt;
  let fileBuffer;
  try {
    occt = await occtimportjs;
    const response = await fetch(url);

    const buffer = await response.arrayBuffer();

    fileBuffer = new Uint8Array(buffer);

    const result = occt.ReadStepFile(fileBuffer, null);

    return result;
  } catch (error) {
    console.error(error);
    throw new Error('Loading geometry error');
  } finally {
    occt = null;
    fileBuffer = null;
  }
};
sookie928 commented 1 year ago

It seems that attaching event listeners to the process might prevent the Garbage Collector from working as expected since they can create references to the events. Modifications may be needed for the following code

process.on('uncaughtException', function (ex) {
  if (!(ex instanceof ExitStatus)) {
    throw ex;
  }
});

process.on('unhandledRejection', function (reason) {
  throw reason;
});