NVS在运行一段时间后,出现在同一个命名空间内,有多个相同的键值出现,导致占用nvs储存空间
代码逻辑是小时冻结数据,一天共冻结24次,第二天将覆盖前一天冻结的数据
下面是我运行的log
I (512822) Main_Log: Namespace: Time_freeze, Key: energy_hour2, Type: 08,number:984
I (513322) Main_Log: Namespace: Time_freeze, Key: energy_hour5, Type: 08,number:985
I (513822) Main_Log: Namespace: Time_freeze, Key: energy_hour5, Type: 08,number:986
I (514322) Main_Log: Namespace: Time_freeze, Key: energy_hour2, Type: 08,number:987
I (514822) Main_Log: Namespace: Time_freeze, Key: energy_hour2, Type: 08,number:988
I (515322) Main_Log: Namespace: Time_freeze, Key: energy_hour5, Type: 08,number:989
在运行了几天后,我打印nvs的状态,发现出现了相同的键值
在官网上没找到nvs可能出现这种状况的原因
所以在此想问问各位大佬有没有遇到这种情况,会是什么原因导致的
感谢
下面是我的写入和读取nvs的逻辑代码
/*************************************************整点冻结*******************************************************************/
// 整点冻结 60分钟冻结一次
void NowTime_freeze_function(void)
{
if (!(strcmp(NowTime_freeze_Str, NowTime_freeze_param)))
{
NowTime_freeze_wirte_data();
}
}
// 整点冻结 写入冻结数据函数
void NowTime_freeze_wirte_data(void)
{
nvs_handle MY_handle; // NVS
esp_err_t err;
err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase()); // 擦除
err = nvs_flash_init();
ESP_LOGE(TAG, "--------------NVS flash擦除");
}
// 打开
err = nvs_open(NowTime_freezeing, NVS_READWRITE, &MY_handle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "opening NVS Error (%s)!\n", esp_err_to_name(err));
}
else
{
ESP_LOGI(TAG, "整点冻结写函数 NVS open OK");
if (!(strcmp(NowTime_freeze_Str, NowTime_freeze_param)))
{
switch (dateTime.hour)
{
case 1:
nvs_set_u64(MY_handle, "energy_hour1", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 2:
nvs_set_u64(MY_handle, "energy_hour2", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 3:
nvs_set_u64(MY_handle, "energy_hour3", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 4:
nvs_set_u64(MY_handle, "energy_hour4", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 5:
nvs_set_u64(MY_handle, "energy_hour5", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 6:
nvs_set_u64(MY_handle, "energy_hour6", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 7:
nvs_set_u64(MY_handle, "energy_hour7", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 8:
nvs_set_u64(MY_handle, "energy_hour8", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 9:
nvs_set_u64(MY_handle, "energy_hour9", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 10:
nvs_set_u64(MY_handle, "energy_hour10", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 11:
nvs_set_u64(MY_handle, "energy_hour11", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 12:
nvs_set_u64(MY_handle, "energy_hour12", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 13:
nvs_set_u64(MY_handle, "energy_hour13", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 14:
nvs_set_u64(MY_handle, "energy_hour14", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 15:
nvs_set_u64(MY_handle, "energy_hour15", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 16:
nvs_set_u64(MY_handle, "energy_hour16", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 17:
nvs_set_u64(MY_handle, "energy_hour17", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 18:
nvs_set_u64(MY_handle, "energy_hour18", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 19:
nvs_set_u64(MY_handle, "energy_hour19", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 20:
nvs_set_u64(MY_handle, "energy_hour20", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 21:
nvs_set_u64(MY_handle, "energy_hour21", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 22:
nvs_set_u64(MY_handle, "energy_hour22", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
case 23:
nvs_set_u64(MY_handle, "energy_hour23", rn8209_value.energy);
ESP_LOGI(TAG, "整点冻结电能为%ld", rn8209_value.energy);
break;
default:
break;
}
}
err = nvs_commit(MY_handle); // 提交写入NVS
if (err != ESP_OK)
ESP_LOGE(TAG, "nvs_commit Error");
nvs_close(MY_handle);
}
}
// 整点冻结 读冻结数据函数 读数据时选择读取哪个时间的整点数据
void NowTime_freeze_read_data_function(uint8_t read_hour)
{
esp_err_t err;
nvs_handle MY_handle;
uint64_t temp = 0; // 临时变量
err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase()); // 擦除
err = nvs_flash_init();
ESP_LOGE(TAG, "--------------NVS flash擦除");
}
err = nvs_open(Daily_freezeing, NVS_READWRITE, &MY_handle);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "opening NVS Error (%s)!", esp_err_to_name(err));
}
else
{
ESP_LOGI(TAG, "整点冻结读函数 NVS open OK");
NowTime_freeze_read_data = 0;
switch (read_hour)
{
case 1:
nvs_get_u64(MY_handle, "energy_hour1", &temp);
NowTime_freeze_read_data = temp;
break;
case 2:
nvs_get_u64(MY_handle, "energy_hour2", &temp);
NowTime_freeze_read_data = temp;
break;
case 3:
nvs_get_u64(MY_handle, "energy_hour3", &temp);
NowTime_freeze_read_data = temp;
break;
case 4:
nvs_get_u64(MY_handle, "energy_hour4", &temp);
NowTime_freeze_read_data = temp;
break;
case 5:
nvs_get_u64(MY_handle, "energy_hour5", &temp);
NowTime_freeze_read_data = temp;
break;
case 6:
nvs_get_u64(MY_handle, "energy_hour6", &temp);
NowTime_freeze_read_data = temp;
break;
case 7:
nvs_get_u64(MY_handle, "energy_hour7", &temp);
NowTime_freeze_read_data = temp;
break;
case 8:
nvs_get_u64(MY_handle, "energy_hour8", &temp);
NowTime_freeze_read_data = temp;
break;
case 9:
nvs_get_u64(MY_handle, "energy_hour9", &temp);
NowTime_freeze_read_data = temp;
break;
case 10:
nvs_get_u64(MY_handle, "energy_hour10", &temp);
NowTime_freeze_read_data = temp;
break;
case 11:
nvs_get_u64(MY_handle, "energy_hour11", &temp);
NowTime_freeze_read_data = temp;
break;
case 12:
nvs_get_u64(MY_handle, "energy_hour12", &temp);
NowTime_freeze_read_data = temp;
break;
case 13:
nvs_get_u64(MY_handle, "energy_hour13", &temp);
NowTime_freeze_read_data = temp;
break;
case 14:
nvs_get_u64(MY_handle, "energy_hour14", &temp);
NowTime_freeze_read_data = temp;
break;
case 15:
nvs_get_u64(MY_handle, "energy_hour15", &temp);
NowTime_freeze_read_data = temp;
break;
case 16:
nvs_get_u64(MY_handle, "energy_hour16", &temp);
NowTime_freeze_read_data = temp;
break;
case 17:
nvs_get_u64(MY_handle, "energy_hour17", &temp);
NowTime_freeze_read_data = temp;
break;
case 18:
nvs_get_u64(MY_handle, "energy_hour18", &temp);
NowTime_freeze_read_data = temp;
break;
case 19:
nvs_get_u64(MY_handle, "energy_hour19", &temp);
NowTime_freeze_read_data = temp;
break;
case 20:
nvs_get_u64(MY_handle, "energy_hour20", &temp);
NowTime_freeze_read_data = temp;
break;
case 21:
nvs_get_u64(MY_handle, "energy_hour21", &temp);
NowTime_freeze_read_data = temp;
break;
case 22:
nvs_get_u64(MY_handle, "energy_hour22", &temp);
NowTime_freeze_read_data = temp;
break;
case 23:
nvs_get_u64(MY_handle, "energy_hour23", &temp);
NowTime_freeze_read_data = temp;
break;
default:
break;
}
nvs_close(MY_handle); // 关闭
}
}
nvs出现相同键值
-
- Posts: 23
- Joined: Wed May 31, 2023 6:54 pm
Re: nvs出现相同键值
Hello,
The NVS subsystem doesn't overwrite the old values in the NVS pages. It marks old value as deleted and finds a free space where it stores the new value. So it works like a running log. At the moment, when no free space is available for new data, the records marked as erased are reclaimed and their space is reused. This is described in detail in the documentation of the nvs component.
https://docs.espressif.com/projects/esp ... #internals
The NVS subsystem doesn't overwrite the old values in the NVS pages. It marks old value as deleted and finds a free space where it stores the new value. So it works like a running log. At the moment, when no free space is available for new data, the records marked as erased are reclaimed and their space is reused. This is described in detail in the documentation of the nvs component.
https://docs.espressif.com/projects/esp ... #internals
Who is online
Users browsing this forum: No registered users and 191 guests