java - GC与其他两种内存管理方式的比较

标签 java c++ c memory-management garbage-collection

我只是想了解更多关于当前流行的垃圾回收malloc/freecounter

据我了解,GC 是最受欢迎的,因为它减轻了开发人员手动管理内存的负担,而且它更安全。 malloc/free 容易出错导致内存泄漏。

来自 http://ocaml.org/learn/tutorials/garbage_collection.html :

Why would garbage collection be faster than explicit memory allocation as in C? It's often assumed that calling free costs nothing. In fact free is an expensive operation which involves navigating over the complex data structures used by the memory allocator. If your program calls free intermittently, then all of that code and data needs to be loaded into the cache, displacing your program code and data, each time you free a single memory allocation. A collection strategy which frees multiple memory areas in one go (such as either a pool allocator or a GC) pays this penalty only once for multiple allocations (thus the cost per allocation is much reduced).

GC 真的比 malloc/free 快吗?

此外,如果counter 风格的内存管理(objective-c 正在使用它)加入派对会怎么样?

希望有人能总结比较,有更深刻的见解。

最佳答案

Is is true that GC faster than malloc / free?

可以。这取决于内存使用模式。它还取决于您如何“更快”地测量。 (例如,您是否测量整体内存管理效率、对 malloc/free 的单独调用,或者……暂停时间。)

但相反,malloc/free 通常比现代复制 GC 更好地利用内存……前提是您不会遇到堆碎片问题。当编程语言没有提供足够的信息以允许 GC 将堆指针与其他值区分开来时,malloc/free“起作用”。

Also, what if the counter style memory management (objective-c is using it) joins the party?

引用计数的开销使指针分配更加昂贵,并且您必须以某种方式处理引用循环。

另一方面,引用计数确实提供了一种控制内存管理暂停的方法……这对于交互式游戏/应用程序来说可能是一个重要问题。而且内存使用也更好;见上文。


FWIW,您引用的来源中的观点是真实的。但这不是全部。

问题是整个图景……太复杂了,无法在 StackOverflow 答案中正确涵盖。

关于java - GC与其他两种内存管理方式的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22739655/

相关文章:

c++ - 我如何在 C++ 中找到堆中的特定索引?

c++ - 覆盖写入控制台的文本

c - 不同返回类型的 gcc 警告

c - 使用 struct 的耗时 C 程序

java - 在 java 中使用 GSON 解析 JSON 并填充 ListView

java - 获取值(value)以增加并在每次增加后返回

Android 应用程序的 Java 编程无法加载

java - 通知后控制不返回到等待线程

c++ - CUDA 将 GpuMat 的 c 数组传递给内核

c - list中存储的对象具有相同的ID或指向同一个对象