I'm writing a shell script to perform a series of actions in remote. Then I want to come back to local and perform the next series of actions. When I use exit, I'm exiting from the shell script instead of logging out of the remote machine.

```
set -x

ssh $1

cd /var/log/sysstat/

for (( i = 11; i <= 19; i++ ))

do

 sar -f $i >> /home/sirish.aditya/cpu_11-19.csv

 sar -f $i -r >> /home/sirish.aditya/mem_11-19.csv

done

exit 

mkdir /Users/sirish.aditya/workSpace/cpustats_srm/$1

scp sirish.aditya@$1:/home/sirish.aditya/mem_11-19.csv /Users/sirish.aditya/workSpace/cpustats_srm/$1

scp sirish.aditya@$1:/home/sirish.aditya/cpu_11-19.csv /Users/sirish.aditya/workSpace/cpustats_srm/$1

```

--

```
ssh sirish.aditya@remote_machine 'bash -s' < memory.sh remote_machine

```

Can somebody help on this?

Thanks in advance
['https://Stackoverflow.com/questions/35524140', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4658006/']
In the example code above, there is circular reference between `A`and `B` classes. These [will be garbage collected](https://stackoverflow.com/a/1910203/3736955) IF they are not reachable by any other object instance in `Application`. 

"`Activity`is destroyed" does not mean it is garbage collected. But when `Activity` is garbage collected, its variables, Views, which only `Activity` references to, will be garbage collected as well. *I think*, on `onDestroy()` of `Activity` class, variables will be assigned to `null` but still their values will be held on memory until they are garbage collected.

In the link written in question, `Presenter` object had reference to `Activity` and vice versa. They would be garbage collected if they were not referenced by other object instances in `Application`. But `Activity` is [referenced by](https://github.com/ribot/android-boilerplate/pull/31) `Presenter` which is referenced by `Subscription`object, which is also referenced by some static class.

Same rules apply to when creating `Adapter` and passing `Context` from `Activity`. If they both are not referenced by other objects, there will not be a leak. But to be cautious, [you should pass](http://android-developers.blogspot.com.tr/2009/01/avoiding-memory-leaks.html) `Application Context`to `Adapter`instead of `Activity Context`.
It would depend on if the object references in A and B are the same object references within each other. After you edited the example to include how A and B were instantiated (newed up), there IS NOT a memory leak in your example because the instances created of each are not the same instances (different memory reference). 

It would be possible to create a memory leak in a case like this, depending on how the two classes are constructed or instantiated. The definition of the classes given above doesn't have enough information to determine if a memory leak is present. 

For example, given the following class definitions:

```
    class A extends Activity{
        private B objectB;

        public void setB(B b){
            objectB = b;
        }
    }

    class B{
        private A objectA;

        public B(A a){
            objectA = a;
        }
    }

```

If you then created a Test class instantiating A and B as shown here:

```
    class Test(){
        public static void main(String... args){
            A a = new A();
            B b = new B(a);
            a.setB(b);
        }
    }

```

In this case, a memory leak has been introduced because as B is instantiated, it is passed a reference to A and then we explicitly set B in A to the same B holding a reference to this instance of A.

If we change the code in Test by one line:

```
    class Test(){
        public static void main(String... args){
            A a = new A();
            B b = new B(a);
            a.setB(new B()); //Modified this line
        }
    }

```

Now we no longer have a memory leak, as the B we pass to A is not the same instance as the B holding a reference to A.