Is it possible to Clear (recover memory from) a specific index to a variable, while leaving other indices to the same variable untouched?
Byte count is 64, for entry in indexed variable. Even if there is no data
As mentioned in comment, use =.
to clear specific index
Too long for a comment:
ByteCount[mytest[data]]
measures the memory required for the expression mytest[data]
. The ByteCount
of a Symbol
is always zero; see the docs for ByteCount
:
Symbols are effectively always shared, so they give 0 byte count:
In other words (I think), it does not count the memory required to store the symbol in the symbol/hash table.
The OP's first ByteCount[data]
computes ByteCount[{1, 2, 3}]
because data
evaluates to the expression {1, 2, 3}
before ByteCount
is called. Consider
Trace[ByteCount@data]
(* {{data, {1, 2, 3}},
ByteCount[{1,2,3}],
112} *)
Also consider the difference between the evaluated and unevaluated data
:
data = {1, 2, 3};
ByteCount /@ {data, Unevaluated@data}
(* {112, 0} *)
@Nasser's example ByteCount[a[1]]
gives 64 bytes and the OP's ByteCount[mytest[data]]
gives 48 bytes (after data
is cleared). Here is an accounting of it:
Clear[a, b];
ByteCount@ 1
ByteCount@ a[b]
ByteCount@ a[1]
(*
16 - storage of one integer
48 - storage of the head--argument tree expression
64 - total
*)
One can carry this accounting game further. For instance, ByteCount@ 2[1]
gives 80
. However, beware that according to the docs,
ByteCount
will...often give an overestimate of the amount of memory...needed....
Back to the principal question, which has already been answered, how to free up memory when a value is no longer needed:
Unset
will remove a single value (definition).Clear
will remove all values (definitions) but not attributes, options, defaults or messages.ClearAll
will remove all values, attributes, options, defaults and messages.Remove
removes the symbol (thereby all things associated with it) from the symbol table.
See the docs for more information. As has been noted in the comments and @Nasser's answer, Unset
is the desired solution in the OP's case.