Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:39:15+00:00 2026-05-27T13:39:15+00:00

What I’m trying to achieve: Reduce memory usage of activity that is no longer

  • 0

What I’m trying to achieve:

  • Reduce memory usage of activity that is no longer on screen, ie another activity has been started
  • The ability for this activity to still be within the navigation stack, so I’m assuming I would have to re-build what was destroyed in onStop, within onStart, but not sure how to do that when all views/buttons were built using layout.xml’s .

The situation:

I have an Android app that is very image heavy, but these images are static on many of layouts, with the same backgrounds, button images, nav headers etc. This led me to build the layouts quite easily without touching code too much by specifying all the imageViews, their src attributes and positions within the layout.xml files. And this worked great, was easy to get up and running, however now there are many reports of force closes due to exceeding the memory usage.

So on in an attempt to cleanup and allow gc to remove images and views that are not on screen, I came across an article (see bottom of question) suggesting within the onDestroy method, grabbing a hold of your root element within the layout and recursively going through the tree removing views and unbinding them. However this only is fired and not guaranteed according to docs when the back buttons is pressed.

So onDestroy does not help me when I am pushing a new activity onto the stack and wan’t to clean up what just left the screen. But the plus side of using the onDestroy method is that the entry point begins at onCreate, so the views are all built up correctly. When I use this method within onStop, memory get’s cleaned up nicely when I start a new activity, but because I’ve nuked all the views, and they were built using layout.xml, I don’t understand how or what I need to re-build in onStart if I destroy everything in onStop especially considering I never created any views in code as they were all setup because of the layout.xml files.

Main Questions: How do I cleanup memory when I start a new Activity? If contexts are handled correctly, is it convention that gc would cleanup all the imageviews that are off screen anyway and re-build them automatically?

Can this be used somehow within onStop ?

 @Override
protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.RootView));
    System.gc();
}

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:39:15+00:00Added an answer on May 27, 2026 at 1:39 pm

    A)
    Using onCreate()/onDestroy() will allocate when activity gets on the stack, and unallocate when activity is popped from the stack.

    B)
    Using onStart()/onStop() will allocate when activity gets visible, and unallocate when activity is not visible.

    Use either A) or B). Don’t mix them or you’ll allocate or deallocate too often.

    In your case, I’d move everything from onCreate() to onStart(), and everything from onDestroy() to onStop().

    Also, consider also using this.setContentView(null) or this.setContentView(new View(this)) in onStop(), to make sure the old views can be garbage collected:

      @Override
      protected void onStop() {
        super.onStop();
        View root = findViewById(R.id.RootView);
        setContentView(new View(this)) 
        unbindDrawables(root);
        System.gc();
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.