P
P
p-oleg2014-11-20 09:59:30
Android
p-oleg, 2014-11-20 09:59:30

Why is memory leaking in Android in a newly created application template?

A question based on a topic on Habré What are memory leaks in android, how to check p... .
Using MAT, in my application I found a memory leak in mContext, but I did not find where exactly. For the experiment, I created a new project in Eclipse, without changing anything in it, launched it as is, and MAT also finds the same memory leak in it! How so?
Freshly created application code:

package com.example.test_mat;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

And here is what MAT shows:
1a1b7a24949f4c5aa5f2bd470b2a7757.gif

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2014-11-27
@cry_san

Try just
public class MainActivity extends Activity
See if the memory leak persists?

D
DevMan, 2017-02-19
@dalmariko

Choate your tasks (as well as their solutions) one more beautiful than the other.
ideone.com/VkmZZx

L
Lenar Shagiev, 2017-02-19
@shagiev

If it is fundamental to use recursion, and if you need one line, then you do not need to do "echo" inside the function, but fasten the lines. More or less like this:

function number_string ($n = 5)
{
    return $n>1 ? number_string($n-1) . $n : $n;
}
echo number_string();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question