初始地址未存储的堆对象是否可以保护垃圾收集? [英] Can a heap object whose initial address is not stored be protected from garbage collection?

查看:97
本文介绍了初始地址未存储的堆对象是否可以保护垃圾收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设


  • 我正在修改别人的C程序;
  • 垃圾收集器是活跃;

  • 堆上存在一个对象,我不希望垃圾收集器收获;和
  • 这个对象会一直存在,直到程序退出,所以不需要释放它($)。


    我必须存储对象的初始地址吗?假设我不在乎最初的地址。相反,我只关心一些指向对象内部的指针,而这些指针都是我存储的。假设我把最初的地址扔掉了。

    垃圾收集器会收获我的对象吗?

    其他信息



    据我所知,该程序现在不会收集垃圾。但是,如果该程序的未来修订版本开始收集垃圾,那么我今天添加的代码可能突然变成难以发现的错误。我不想做出一个难以发现的错误;但该计划是一个老的,稳定的计划,成千上万的用户使用了多年。因此该程序在各种实际条件下都可以接受地工作。重新设计不是一个选项。



    该程序采用全球数据结构,它永远不会释放()。这是我必须工作的设计。



    如果您想知道:我希望存储的指针 - 指向其对象内部的指针 - 碰巧指向ASCII字符串中的单词。我只关心这些字,而不是关于整个字符串。特别是,我不关心字符串开头的空格,这就是为什么我不关心字符串的初始地址;但垃圾收集器可能会无意中关心,可能不是吗?

    似乎很难存储一个链接的指针列表,这个指针既不是我也不是其他人会用的,只是为了防止关闭一个不存在的假设垃圾收集器;但如果真的有必要,我会保存清单。



    或者我的担心是没有根据的?无论如何,没有人会向旧C程序添加垃圾回收功能吗?

    解决方案

    让我们抛开一个事实:使用 allocate()但不是 free()的程序几乎不能称为stable;

    让我们抛开一个事实,那就是将垃圾收集添加到现有的大型C程序中就是其中一种如果你去的话,你会发现只有痛苦情况。



    您的问题的答案是:

    你的垃圾收集器的工作方式。



    如果它是异国情调,它可能会扫描内存并寻找指向堆中任何位置的指针,而不仅仅是开始的内存块。在这种情况下,你会被覆盖,因为指向字符串中间的指针将足以保持字符串锚定在内存中。 (防止它被垃圾收集。)



    如果它不是特别的,那么它只会寻找指向内存块开始的指针。 (这是一个相当明智的做法。)在这种情况下,不,你的对象不会被锚定,除非你维护一个指向对象本身的指针。



    就我个人而言,我甚至不会尝试一个异国情调的垃圾收集器,但那只是我。


    Suppose that

    • I am modifying someone else's C program;
    • a garbage collector is active;
    • there exists an object on the heap I do not want the garbage collector to reap; and
    • the object lives until the program exits, so it is unnecessary to free() it.

    Must I store the object's initial address? Suppose that I don't care about the initial address. Suppose instead that I only care about some pointers into the object's interior, and that these pointers are all I store. Suppose that I throw the initial address away.

    Will the garbage collector reap my object?

    ADDITIONAL INFORMATION

    The program does not now collect garbage as far as I know. However, if a future revision of the program began to collect garbage, then the code I am adding today might suddenly turn into a hard-to-find bug. I don't want to make a hard-to-find bug; but the program is an old, stable program thousands of users have used for many years. The program is thus known to function acceptably under a wide variety of real conditions. Redesign is not an option.

    The program employs global data structures it never bothers to free(). This is the design within which I must work.

    If you want to know: the pointers I wish to store—the pointers that point into their objects' interiors—happen to point to words within an ASCII string. I care only about the words, not about the whole string. Especially, I don't care about whitespace at the start of the string, which is why I don't care about the string's initial address; but a garbage collector might inadvertently care, mightn't it?

    It seems silly to store a linked list of pointers neither I nor anyone else will ever use, just to fend off a hypothetical garbage collector that does not exist; but I'd store the list if truly necessary.

    Or is my concern groundless? Does no one ever add garbage collection to old C programs, anyway?

    解决方案

    Let us set aside the fact that a program that uses allocate() but not free() can hardly be called "stable";

    Let us set aside the fact that adding garbage collection to an existing large C program that works is one of those "if there you go, only pain will you find" situations.

    The answer to your question is:

    It depends on how your garbage collector works.

    If it is exotic, it may be sweeping memory and looking for pointers that point anywhere within the heap, not just to beginnings of memory blocks. In this case, you are covered, since a pointer pointing in the middle of a string will be enough to keep the string anchored in memory. (Prevent it from being garbage-collected.)

    If it is not that exotic, then it will only be looking for pointers that point to beginnings of memory blocks. (Which is a rather sensible thing to do.) In which case, no, your object is not anchored unless you maintain a pointer to the object itself.

    Personally, I wouldn't even try an exotic garbage collector, but that's just me.

    这篇关于初始地址未存储的堆对象是否可以保护垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆