Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a possible way to clear the arguments passed to a module to release some memory. I believe that arguments passed to modules are passed by value rather than pass by reference.

e.g. I have the following module

DoSomething[arglist_,arglist2_]:=Module[{localvar},
Some long computations...
(*Clear arglist here*)
Some further long computations...
(*Is it possible to reassign some other value to arglist here? e.g. arglist={1,2}*)
];

I've tried to use Clear[arglist] but i've got the following Clear::ssym error, however Clear[localvar] was fine. All I wan't is to release some memory because my arglist_ is a list of million sublists and tens of elements in each sublist.

Secondly Is it possible to reassign some other value to arglist here? e.g. arglist={1,2} within a module.

Thanks in advance for the help.

share|improve this question
    
Are you sure the use of arglist_ (as a named pattern) assigns a value to variable 'arglist' (that you need to clear)? –  Wouter Jun 12 at 16:57
    
@Wouter yes that's how im using it for a long time, without any trouble. i.e. DoSomething[{{1},{2}},{4,5}]; –  aark Jun 12 at 18:43
    
Your belief isn't correct. You are formally right, in that formally arguments are passed by value. Effectively, however, most arguments are passed by reference, because most arguments are immutable expressions. Even if passed arguments are L-values and can be modified, still the references are passed, and no extra copying is done until you actually modify those expressions. It would be insane for Mathematica to do it differently, given its strong emphasis on immutability. –  Leonid Shifrin Jun 12 at 18:57
    
Mathematica does not simply "pass by value" or "pass by reference" as in C. Data structures are copied only when they're modified. There's never any need to try to clear arguments. Function calls don't make in-memory copies. Only modifications do. en.wikipedia.org/wiki/Copy-on-write (Some hints about this in the docs.) –  Szabolcs Jun 12 at 19:00
1  
Maybe this? [(mathematica.stackexchange.com/questions/17767/… –  eldo Jun 12 at 19:17

2 Answers 2

DoSomething[arglist_, arglist2_] := Module[{localvar}, {First[arglist], Last[arglist2]}]; 
DoSomething[{7, 1}, {it, 0, nothing}]  

produces

{7, nothing}  

Now look at :

?? arglist  

and you see that is a Global variable without any assigned value (that you could want to clear).
Now, since variables like 'x' are heavily used, simultaneously as function arguments as 'x_' and as bona fide arguments as in 'a x^2+b x +c ==0 ' without any carry-over from the latest value passed by 'x_' to some function, there is nothing gained by clearing 'x' after every argument-passing-job, is there?
Of course, things get more complicated if we combine an argument passing activity with a 'Set' instruction, either globally or locally (inside a Block or Module). But your question did not venture there.

share|improve this answer

When you "call" DoSomething, Mathematica does not assign local variables (as Module does)), but directly substitutes the passed values into the expression (similar to how With works). You can see that with the following code:

foo[x_]:=Module[{y=2}, Hold[x,y]]
foo[1]
(*
==> Hold[1, y$81]
*)

As you can see, since Hold prevents evaluation, the local variable generated by Module (y$81 in this case) is kept. However, the x is replaced by 1. Since Hold prevents evaluation of the arguments, it is clear that this has been done before evaluation of the expression Hold[x,y].

Looking closer, you'll also notice that y$81 also isn't the same as y: Also Module replaces the variable, but not by the given value, but with a temporary variable which gets assigned the value. However, With will give the same effect as the argument, direct substitution:

bar[x_]:=With[{y=2}, Hold[x,y]]
bar[1]
(*
==> Hold[1, 2]
*)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.