Tom*_*miT 8 arrays garbage-collection d heap-memory
我想从垃圾收集堆中分配一个元素数组,并只通过原始指针访问这些元素.垃圾收集器是否能够在用于指向它的所有指针超出范围之后(而不是之前)回收该内存块?
我想这样做:
{
int* ptrToArray1 = (new int[](100)).ptr;
int* ptrToArray2 = ptrToArray1;
int* ptrToArray3 = ptrToArray1 + 10;
ptrToArray1 += 50;
ptrToArray2 += 99;
*ptrToArray1 = 123;
*ptrToArray2 = 456;
*ptrToArray3 = 789;
ptrToArray1 -= 42;
ptrToArray2 -= 24;
//... and so on ... Is the array data guaranteed to be there?
}
// Now that all the pointers are out of scope, can the
// garbage collector reclaim the memory block of that array?
Run Code Online (Sandbox Code Playgroud)
小智 14
您的方案将有效.
两件事情:
值得注意的是,内部指针显着减慢了垃圾收集器的标记阶段,但在像D这样的系统语言中,不支持内部指针是不合理的.
最后,请注意,如果将指向GC分配的内存的指针存储在GC堆和堆栈/寄存器之外,GC 将不会将其捕获.也就是说,如果你将一些数组存储.ptr在某个malloc内存中,然后丢弃对它的所有引用,那么它就不会被认为是实时的.
| 归档时间: |
|
| 查看次数: |
257 次 |
| 最近记录: |