回答tracemem recently后,我了解了retracemem?retracemem的帮助以及其中的示例使我无所适从。

retracemem实际做什么,为什么?它并没有达到我的预期,即至少将一个对象指向另一个对象的存储位置。

我尝试了.Internal(inspect())这样的向导式调用,该调用对我没有任何作用:

> a = 1:10
> b = a[-1]
> .Internal(inspect(a))
@00000000087AE578 13 INTSXP g0c4 [NAM(2)] (len=10, tl=23336) 1,2,3,4,5,...
> .Internal(inspect(b))
@00000000087AE8E8 13 INTSXP g0c4 [NAM(2)] (len=9, tl=7208) 2,3,4,5,6,...
> retracemem(b,retracemem(a))
> .Internal(inspect(b))
@00000000087AE8E8 13 INTSXP g0c4 [NAM(2)] (len=9, tl=7208) 2,3,4,5,6,...

最佳答案

我认为retracemem()仅允许您标记变量副本,而不会产生tracemem语句(例如,上面的b实际上只是a的副本,没有第一个元素),因为它是从相同的源变量,因此您可以继续观察副本/部分副本的传播,并查看它们源自同一源。

例如,A的内存如何复制/传播:

> A <- 1:10
> tracemem(A)
[1] "<0x100a2a978>"
> B <- A                                # Assignment to B doesn't make copy
> C <- A + 1                            # Assignment to C makes copy, alters it
tracemem[0x100a2a978 -> 0x1020ebbf0]:
> D <- C + 1                            # Assignment to D makes copy, alters it
tracemem[0x1020ebbf0 -> 0x1020ebc98]:
> E <- B + 1                            # Assignment to E makes copy, alters it
tracemem[0x100a2a978 -> 0x1020a4208]:
> F <- A[-1]                            # Assignment to F doesn't make copy?
> G <- F + 1                            # Even after altering it?
> retracemem(F, retracemem(A))          # Hint to R that F is really A derived
tracemem[<0x100a2a978> -> 0x1009c5910]:
> G <- F + 1                            # Reassignment to G makes copy, alters it
tracemem[0x1009c5910 -> 0x1020a4748]:


但是话又说回来,我可能完全错了。

08-24 12:58