Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android -- Why my app uses about 40MB Cached background process?

I'm starting a new app with minSdkVersion="14" and targetSdkVersion="17". It contains a viewpager with 6 pages. There is 3 webviews and 3 others views.

When i push my app to background by clicking on back or home button, it uses about 40MB in "cached background process" and i don't understand why.

This is an example of one of my webview :

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

public class Presentation extends Fragment {
    boolean isOption = false;
    RelativeLayout main = null;
    WebView web_main = null;

    public Presentation () {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        main = (RelativeLayout) inflater.inflate(R.layout.webview,  container, false);
        return main;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        web_main = new WebView(getActivity().getApplicationContext());
        web_main.setWebViewClient(new WebViewClient());
        web_main.getSettings().setAppCacheEnabled(false);
        web_main.loadUrl("file:///android_asset/main.html");
        main.removeAllViews();
        main.addView(web_main);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(getClass().getName(), "[OnDestroy]");
        main.removeAllViews();
        web_main.destroy();
        main = null;
        web_main = null;
        System.gc();
    }

}

I followed several tutorials and answers but there are no effect on cached background process This is my main activity :

public class AppTest extends FragmentActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
protected void onStop() {
    super.onStop();
    System.gc();
    Log.i(getClass().getName(), "[OnStop]");
    android.os.Debug.stopMethodTracing();

}


@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_content, menu);
    return true;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mViewPager.removeAllViews();
    Log.i(getClass().getName(), "[OnDestroy]");
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = null;

        switch (position) {
        case 0:
            fragment = new Presentation();
            break;
              /*    case 1:
            fragment = new Edition();
            break;
        case 2:
            fragment = new Programme();
            break;
        case 3:
            fragment = new Twitter();
            break;
        case 4:
            fragment = new Partenaire();
            break;
        case 5:
            fragment = new Information();
            break;*/
        default:
            fragment = new Presentation();
            break;
        }


        return fragment;
    }

    @Override
    public int getCount() {
        // Show 6 total pages.
        return 6;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {
        case 0:
            return "Presentation";
        case 1:
            return "Edition";
        case 2:
            return "Program";
        case 3:
            return "Tweets";
        case 4:
            return "Partners";
        case 5:
            return "Information";
        }
        return null;
    }
}
}

Can anyone see what is wrong ?

EDIT I tried to put webview in layout but it's still the same In fact, i want to know what is put in cache when app is in background state ?

like image 574
TraeX Avatar asked Mar 28 '13 17:03

TraeX


People also ask

What does cached background process mean Android?

That being said, "cached background processes" usually refers to processes that do not have a foreground activity and do not have a running service. These processes are kept in memory simply because we have enough memory to do so, and therefore, as you note, the user can switch back to these processes quickly.

Do cached processes drain battery?

While apps running in the background would cause battery drain, cached processes are not active processes and therefore don't need to be shut down, contrary to what a task killer does.


1 Answers

"Cached background processes" usually refers to processes that do not have a foreground activity and do not have a running service. These processes are kept in memory because we have enough memory and we can allow the user to switch back to them quickly. If Android starts running out of RAM, these processes will be the first ones to be destroyed to free up RAM. Sometimes, an old app process might be kept around when the same app is switched to a new process.

As far as I can tell, the space occupied in the "cached background process" state is going to be determined by whatever your app is currently using. For example, if an app is using 20MB in the foreground, then if RAM is available, the same amount of space will be occupied.

If your app has 3 ImageViews and 3 WebViews, it can very well occupy 40MB of RAM space depending on what's stored in those ImageViews and WebViews. You can use Profiling tools to see how much memory your app is using and what are it's components. If the memory in used during the foreground is similar to the one when in background state, then everything is as it should be.

Note: Manufacturers can mess with the settings app and redefine what is meant by "cached background process". In that case, You'll have to contact them to see how exactly they define it and what it is composed of.

like image 87
Anup Cowkur Avatar answered Nov 16 '22 02:11

Anup Cowkur