06-21-2020 22:59
06-21-2020 22:59
Hi, I'm trying to save few data in an object each five minutes, the proble is sometimes appears this error
Fatal Jerryscript Error: ERR_OUT_OF_MEMORY, I tried to delete each property of the object later I sent the data to a json.txt, How could i resolve the error ?
this is my code, I'm new with js, i would appreciate your help
thanks
import clock from "clock";
import document from "document";
import { preferences } from "user-settings";
import * as util from "../common/utils";
import * as fs from "fs";
import { HeartRateSensor } from "heart-rate";
import { user } from "user-profile"; // acceder a la información del usuario
import { me as appbit } from "appbit"; // acceder a la información que se suministra en today
import { today } from "user-activity"; //actividad del usuario
import { memory } from "system";
const startingMinutes = 1
let time = startingMinutes * 60
const $countdownE1 = document.getElementById('countdown')
const $corazon = document.getElementById('heartRate')
var persona = {
}
clock.granularity = "minutes";
const myLabel = document.getElementById("myLabel");
clock.ontick = (evt) => {
let today = evt.date;
let hours = today.getHours();
let minutes = today.getMinutes()
if (preferences.clockDisplay === "12h") {
// 12h format
hours = hours % 12 || 12;
} else {
// 24h format
hours = util.zeroPad(hours);
}
let mins = util.zeroPad(today.getMinutes());
myLabel.text = `${hours}:${mins}`;
}
setInterval(updateCountdown, 1000)
function updateCountdown(){
const minutes = Math.floor(time / 60)
let seconds = time % 60
seconds = seconds < 10 ? '0' + seconds : seconds
$countdownE1.text = `${minutes}: ${seconds}`
averageHeartRate()
time--
time = time < 0 ? 0: time
if (minutes === 0 && String(seconds) === '00'){
$countdownE1.text = 'LLEGAMOS A 5'
startingMinutes = 1
time = startingMinutes * 60
makePerfil(persona)
}
}
function averageHeartRate() {
const hrm = new HeartRateSensor();
hrm.addEventListener("reading", () => {
$corazon.text = hrm.heartRate
hrm.stop()
});
hrm.start();
}
function makePerfil(persona) {
persona = {
...persona,
edad: user.age,
genero: user.gender,
altura: user.height,
peso: user.weight,
}
sensorsInformation(persona)
}
function sensorsInformation(persona){
if (appbit.permissions.granted("access_activity")) {
persona = {
...persona,
minutosActivo: today.adjusted.activeMinutes,
calorias: today.adjusted.calories,
distance: today.adjusted.distance,
steps: today.adjusted.steps,
elevationGain: today.adjusted.elevationGain
}
}
averageHeartRate2(persona)
}
function averageHeartRate2(persona) {
const hrm = new HeartRateSensor();
hrm.addEventListener("reading", () => {
persona = {
...persona,
frecuenciaCardiaca: hrm.heartRate,
}
hrm.stop()
takeTheTime(persona)
});
hrm.start();
}
function takeTheTime(persona){
var today = new Date()
persona = {
...persona,
fechaYHora: today
}
console.log(today)
console.log("JS memory: " + memory.monitor.pressure );
console.log(`${persona.genero} ${persona.edad} ${persona.altura} ${persona.peso} ${persona.minutosActivo} `)
console.log(`${persona.calorias} ${persona.distance} ${persona.steps} ${persona.elevationGain}`)
delete persona.minutosActivo
delete persona.calorias
delete persona.distance
delete persona.steps
delete persona.elevationGain
delete persona.edad
delete persona.genero
delete persona.altura
delete persona.peso
delete persona.frecuenciaCardiaca
delete persona.fechaYHora
}
Answered! Go to the Best Answer.
06-22-2020 05:24
06-22-2020 05:24
Hello @Yahuarcani ,
I think you are adding an event listener here with every execution of the interval.
You can have multiple listeners to events, but you will eventually run out of memory if you add to many.
You may want to add one listener and trigger your cyclical code from there, or fill a global var that your setinterval code can process.
Regards,
Reign
06-21-2020 23:21
06-21-2020 23:21
I rarely use the 'spread' operator (...), but you may be using it iteratively, with the result that persona grows in size more that you're expecting.
In particular, think about persona={...persona which you have in a few places.
Wherever you do this to persona, it would be interesting to follow it with console.log(JSON.stringify(persona)) just to see what it's doing.
It's even possible that persona={...persona is recursive, which won't be very memory-efficient.
If you just want to add/update persona attributes, you don't need to reinitialise the object.
But I could be way off track...
06-22-2020 05:24
06-22-2020 05:24
Hello @Yahuarcani ,
I think you are adding an event listener here with every execution of the interval.
You can have multiple listeners to events, but you will eventually run out of memory if you add to many.
You may want to add one listener and trigger your cyclical code from there, or fill a global var that your setinterval code can process.
Regards,
Reign
06-22-2020 07:26 - edited 06-22-2020 07:27
06-22-2020 07:26 - edited 06-22-2020 07:27
Hello Peter,
I think your point about the spread operator is also correct.
There are some things that just don't work easily without objects, but there is always a memory cost. When objects or object manipulation gets complex, that cost can rise surprisingly fast.
In this case adding an element to an existing array may be more efficient. I think the spread here is actually, temporarily creating a copy of the persona object every time a reading is added. Most will eventually be deleted, but javaScript garbage collection is interesting.
The methods that can and often are used in larger systems simply won't fit into a watch.
Regards,
Reign
06-23-2020 09:40
06-23-2020 09:40
Really thank you, I could achieve it, i used the memory more efficient, @Gondwana, @morningReign.
if you have other advice about my code, I'd appreciate you