在 Pebble 中,exit() 上的 free()ing 内存真的有必要吗?
Is free()ing memory on exit() really necessary in Pebble?
在 More C Concepts (Google webcache,而不是 Internet Archive),我阅读了以下内容:
When the app is exiting or when the buffer is no longer needed, the memory must be freed for applications to use:
// Free dynamically allocated memory
free(s_buffer);
在Learning C for the Pebble SDK (Internet Archive), 我读到:
Unlike other languages (such as Python or JavaScript), the C language relies on the developers to manage any memory they use. This means that any memory allocated by a program for storing data must be freed up again by the developer so that other progams can make use of it.
我习惯于在非嵌入式场景中编程,其中每个应用程序都有自己的地址 space 并且在进程终止时 OS 回收分配的内存。然而,Pebble SDK 的上述两个语句表明,如果我调用 malloc()
然后程序在调用 free()
之前退出或被杀死,手表将无法回收内存。它还显示了在事件循环退出等后取消初始化 windows 的示例
我真的希望如果我需要在启动时动态计算一次缓冲区大小,我就不必担心在进程退出之前清理它。当然,对于生命周期短于进程的东西,我需要跟踪它们并清理它们以避免泄漏并导致我的应用程序 运行 内存不足。但是尝试为 一切 执行此操作——而且,更重要的是,以一种确保 free()
或 *_deinit()
被调用的方式,即使程序不干净地终止——似乎既浪费时间又不可能。
如何安全地编写 Pebble Watch 应用程序,以便在应用程序被终止时释放所有分配的内存?还是 Pebble 的 OS 会像现代 OSes 一样跟踪和清理应用程序终止时分配的资源?
答案好像是:
- 是的,如果您的应用程序退出,无论是通过终止还是正常退出,一切都将在您之后清除
- 确保 0B 报告为在退出时仍分配是一个好习惯,因为它有助于防止内存泄漏。
如您所见,官方文档几乎只字未提这个主题,因此如果没有 Pebble 开发人员的声明,我们将无法获得明确的答案。不过,我们有几个接近的选择:第一个是转向 Pebble 论坛:
我们还可以检查最可靠的来源,Reddit 的 /r/pebbledevelopers,他声称:
- "the entire process will be killed on exit, so you don't technically have to worry about it. Cleaning up after yourself is a nice habit to get into, though."
- "When your app is closed, all memory it had allocated is freed. Strictly speaking, this means that deallocating things in your main window isn't necessary. However, making sure 0 bytes are allocated when you exit the app helps you prevent future problems."
- "all the memory you allocated is released by the system when your app is closed, so there's really no reason to shut it down."
- "It actually doesn't matter if you have memory still allocated when your watchface quits. The system frees up all the memory that you use automatically"
我们也可以查看 this set of slides,特别是:
(我相信这只适用于 Aplite,a.k.a。Pebble 1.O)
这表明应用程序不多 "headroom" -- 如果一个或多个应用程序开始泄漏并且没有被 OS 完全清理,它很快就会无法启动新应用。
总而言之:我不会太担心它。
在 More C Concepts (Google webcache,而不是 Internet Archive),我阅读了以下内容:
When the app is exiting or when the buffer is no longer needed, the memory must be freed for applications to use:
// Free dynamically allocated memory free(s_buffer);
在Learning C for the Pebble SDK (Internet Archive), 我读到:
Unlike other languages (such as Python or JavaScript), the C language relies on the developers to manage any memory they use. This means that any memory allocated by a program for storing data must be freed up again by the developer so that other progams can make use of it.
我习惯于在非嵌入式场景中编程,其中每个应用程序都有自己的地址 space 并且在进程终止时 OS 回收分配的内存。然而,Pebble SDK 的上述两个语句表明,如果我调用 malloc()
然后程序在调用 free()
之前退出或被杀死,手表将无法回收内存。它还显示了在事件循环退出等后取消初始化 windows 的示例
我真的希望如果我需要在启动时动态计算一次缓冲区大小,我就不必担心在进程退出之前清理它。当然,对于生命周期短于进程的东西,我需要跟踪它们并清理它们以避免泄漏并导致我的应用程序 运行 内存不足。但是尝试为 一切 执行此操作——而且,更重要的是,以一种确保 free()
或 *_deinit()
被调用的方式,即使程序不干净地终止——似乎既浪费时间又不可能。
如何安全地编写 Pebble Watch 应用程序,以便在应用程序被终止时释放所有分配的内存?还是 Pebble 的 OS 会像现代 OSes 一样跟踪和清理应用程序终止时分配的资源?
答案好像是:
- 是的,如果您的应用程序退出,无论是通过终止还是正常退出,一切都将在您之后清除
- 确保 0B 报告为在退出时仍分配是一个好习惯,因为它有助于防止内存泄漏。
如您所见,官方文档几乎只字未提这个主题,因此如果没有 Pebble 开发人员的声明,我们将无法获得明确的答案。不过,我们有几个接近的选择:第一个是转向 Pebble 论坛:
我们还可以检查最可靠的来源,Reddit 的 /r/pebbledevelopers,他声称:
- "the entire process will be killed on exit, so you don't technically have to worry about it. Cleaning up after yourself is a nice habit to get into, though."
- "When your app is closed, all memory it had allocated is freed. Strictly speaking, this means that deallocating things in your main window isn't necessary. However, making sure 0 bytes are allocated when you exit the app helps you prevent future problems."
- "all the memory you allocated is released by the system when your app is closed, so there's really no reason to shut it down."
- "It actually doesn't matter if you have memory still allocated when your watchface quits. The system frees up all the memory that you use automatically"
我们也可以查看 this set of slides,特别是:
(我相信这只适用于 Aplite,a.k.a。Pebble 1.O)
这表明应用程序不多 "headroom" -- 如果一个或多个应用程序开始泄漏并且没有被 OS 完全清理,它很快就会无法启动新应用。
总而言之:我不会太担心它。