Question: My main activity code is :

```
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/Container"> </LinearLayout>

```

And My dynamic layout code is:

```
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView sv = new ScrollView(this);

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

    TextView tv = new TextView(this);
    tv.setText("Dynamic layouts ftw!");
    ll.addView(tv);

    EditText et = new EditText(this);
    et.setText("weeeeeeeeeee~!");
    ll.addView(et);

    Button b = new Button(this);
    b.setText("I don't do anything, but I was added dynamically. :)");
    ll.addView(b);

    for(int i = 0; i < 20; i++) {
        CheckBox cb = new CheckBox(this);
        cb.setText("I'm dynamic!");
        ll.addView(cb);
    }
    this.setContentView(sv);
}

```

i want to add the dynamically coded View inside the LinearLayout with Id=Container. How to do that

Answer: 
found the solution

I replaced

```
$instReader->disconnectWorksheets();
unset($instReader);

```

with 

```
$instReader->__destruct();
unset($instReader);

```

and all seems good.
I was able to fix my memory leak using the accepted answer <https://stackoverflow.com/a/27766461/4912853> and a tip from: [Dump all variables in PHP](https://stackoverflow.com/questions/24793064/dump-all-variables-in-php#comment38482425_24793064):

> 
> PHP frees the memory at the end of a function, hence `the_loop()`
> 
> 
> 

Before:

```
foreach($rows as $row){
    ...
    $instReader->disconnectWorksheets();
    unset($instReader);
}

```

After:

```
function createExcelFile($row){
    ...
    $instReader->__destruct();
    unset($instReader);
}

foreach($rows as $row){
    createExcelFile($row);
}

```

The above claim could definitely use a source but these 2 ideas together completely fixed my memory issues after much trial and error. Hope this helps!