Whether a term bound to global variable is reclaimed when the pointer is rewritten?

Suppose this.

:- functor(Big, f, 10), nb_setval(bigarray, Big).
?- b_getval(bigarray, Y), X=a, setarg(1, Y, X),
	b_setval(bigarray, []), garbage_collect, write(X).

My question is whether the big term bound to bigarray is reclaimed by the garbage_collect call. My experience seems to tell me ‘Yes’, according to the activity monitor of macOS, but I am not sure on this. Although some argument of the bigarray referred, no pointer to bigarray exists
after the b_setval(bigarray, []). So I think it should be reclaimed.

Just using b_setval/2 to [] is not enough. After backtracking over this it needs to restore the old value, so the value is part of the trail. If there are no choicepoints between the creation of the global variable and its modifications, we can safely discard. I think the system properly takes care of that, though it might be wise to test in a loop and see whether the stack stays small.

It surely merges multiple assignments to the same location that have no choice point in between. This scenario only keeps the last assignment in the trail, making the older values subject to GC.

Thanks. I am aware of not leaving choice point, but the experimental result is not what I expected. I must review my relevant codes with experiments you suggest.

Watching activity monitor on memory usage, b_setval seems to cause the trail stack increasing (slowly). Although still I am not clear on reclaiming terms in Prolog, appropriate use of cut (!) and garbage_collect/0 seems to fit with my use of big arrays, otherwise Prolog process easily consumes more than 200GB (virtual memory).

% ?- gc_test(100, 100000000).
%@ true.

gc_test(I, N):- I>0,  % writeln(I),
		 functor(Big, f, N),
		  nb_setval(big, Big),
		  b_getval(big, Big0),
		  setarg(1, Big0, []),
		  I0 is I - 1,
		  garbage_collect,    % optional
		  !,                             % optional
		  gc_test(I0, N).
gc_test(_, _).