'Does this function leak memory
I have this program that parses a json file and assigns the keys and values to two arrays. Then it loops through each key, reads a css file and if there is a match in the css variables it rewrites the file with the corresponding value.
const fs = require('fs')
const path = require('path')
function applyConfigurationToCss() {
let str = '';
let regex = new RegExp(`--${str}: .*$`, 'gm')
let theme_keys = []
let theme_values = []
let css_file = fs.readFileSync('../style.css', 'utf-8')
const key_value_pairs = JSON.parse(fs.readFileSync('../theme.json'), (key, value) => {
theme_keys.push(key)
theme_values.push(value)
})
for (let i = 0; i < theme_keys.length - 2; i++) {
str = theme_keys[i]
console.log(str)
regex = new RegExp(`--${str}: .*$`, 'gm')
fs.writeFileSync('../style.css', css_file.replace(regex, `--${str}: ${theme_values[i]};`), 'utf-8')
css_file = fs.readFileSync('../style.css', 'utf-8')
console.log(process.memoryUsage())
}
}
applyConfigurationToCss()
It works like it should for now, but on each cycle of the for loop the heapUsed value grows. I am just now looking more into memory management and I was wondering if this is a sign of a memory leak. Here is the program output:
color0
{
rss: 34164736,
heapTotal: 4980736,
heapUsed: 4240312,
external: 452419,
arrayBuffers: 19350
}
color1
{
rss: 34615296,
heapTotal: 5513216,
heapUsed: 4778160,
external: 452459,
arrayBuffers: 19350
}
color2
{
rss: 34615296,
heapTotal: 5513216,
heapUsed: 4791600,
external: 460651,
arrayBuffers: 27542
}
color3
{
rss: 34615296,
heapTotal: 5513216,
heapUsed: 4805008,
external: 468843,
arrayBuffers: 35734
}
bg-img
{
rss: 34615296,
heapTotal: 5513216,
heapUsed: 4815472,
external: 468843,
arrayBuffers: 35734
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
