You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello is there any other way to close an open file after reading it as a string? My case is after loading a big file (800 MB), the memory goes high (up to +700 MB), and even when try to open the file again the memory keeps multiplicity (going around from 1.4 to 2.8 and so on). Seems like the variable contains the string from the opened file held inside the memory.
Here’s my example of code
/**
* Get Irradiation value from the given latitude and longitude
* @param {Object} params
* @param {number} params.lat - Latitude
* @param {number} params.long - Longitude
* @returns Irradiation value
*/
async getIrradiation({ lat, long }: { lat: number; long: number }) {
// File size is 800++ MB
const filePath = "public/irradiation/GHI.asc";
try {
const file = Bun.file(filePath);
const isFileExists = await file.exists();
if (!isFileExists) {
throw new NOT_FOUND_ERROR("Irradiation file not found!");
}
const fileContent = await file.text();
const lines = fileContent.split("\n");
// Extract metadata
const metadata: string[] = lines.slice(0, 5);
const dataMeta: MetadataIrradiation = {};
for (const meta of metadata) {
const [key, value] = meta.split(" ");
dataMeta[key] = Number.parseFloat(value.replace(/[^0-9,.-]/g, ""));
}
// Calculate bounds and positions
const northBound = dataMeta.nrows * dataMeta.cellsize + dataMeta.yllcorner;
const eastBound = dataMeta.xllcorner;
const getRow = Math.ceil(Math.abs((northBound - lat) / dataMeta.cellsize) + 6);
const getCol = Math.ceil(Math.abs((eastBound - long) / dataMeta.cellsize));
if (getRow > lines.length) {
moduleLogger.error("Calculated row exceeds file length");
return;
}
const lineLat = lines[getRow - 1];
let calculateIrradiation: number | undefined;
if (lineLat) {
const tokens = lineLat.split(" ");
const irradiation = Number.parseFloat(tokens[getCol - 1]);
if (!Number.isNaN(irradiation) && irradiation !== dataMeta.NODATA_value) {
calculateIrradiation = irradiation * 365;
}
}
moduleLogger.warn(`Calculated Irradiation: ${calculateIrradiation}`);
return calculateIrradiation;
} catch (err) {
moduleLogger.error(err, "Unable to open file!");
}
},
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello is there any other way to close an open file after reading it as a string? My case is after loading a big file (800 MB), the memory goes high (up to +700 MB), and even when try to open the file again the memory keeps multiplicity (going around from 1.4 to 2.8 and so on). Seems like the variable contains the string from the opened file held inside the memory.
Here’s my example of code
Beta Was this translation helpful? Give feedback.
All reactions