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 3312490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:00:06+00:00 2026-05-17T22:00:06+00:00

It seams that Dalvik’s garbage collector doesn’t respect SoftReferences and removes them as soon

  • 0

It seams that Dalvik’s garbage collector doesn’t respect SoftReferences and removes them as soon as possible, just like WeakReferences. I’m not 100% sure yet, but despite the fact that there is still ~3MB of free memory my SoftReferences get cleared after I see “GC freed bla-bla-bla bytes” in LogCat.

Also, I saw a comment by Mark Murphy here:

Except that it doesn’t work on
Android, at least in the 1.5
timeframe. I have no idea if the GC
SoftReference bugs have been fixed.
SoftReferences get GC’d too soon with
this bug.

Is it true? Are SoftReferences not respected?

How to workaround this?

  • 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-17T22:00:07+00:00Added an answer on May 17, 2026 at 10:00 pm

    After not receiving an answer I decided to make my own study. I’ve made a simple test to exercise the GC against SoftReferences.

    public class TestSoftReference extends TestCase {
    
        public void testSoftRefsAgainstGc_1() {        testGcWithSoftRefs(1);    }
    
        public void testSoftRefsAgainstGc_2() {        testGcWithSoftRefs(2);    }
    
        public void testSoftRefsAgainstGc_3() {        testGcWithSoftRefs(3);    }
    
        public void testSoftRefsAgainstGc_4() {        testGcWithSoftRefs(4);    }
    
        public void testSoftRefsAgainstGc_5() {        testGcWithSoftRefs(5);    }
    
        public void testSoftRefsAgainstGc_6() {        testGcWithSoftRefs(6);    }
    
        public void testSoftRefsAgainstGc_7() {        testGcWithSoftRefs(7);    }
    
    
        private static final int SR_COUNT = 1000;
    
        private void testGcWithSoftRefs(final int gc_count) {
            /* "Integer(i)" is a referrent. It is important to have it referenced
             * only from the SoftReference and from nothing else. */
            final ArrayList<SoftReference<Integer>> list = new ArrayList<SoftReference<Integer>>(SR_COUNT);
            for (int i = 0; i < SR_COUNT; ++i) {
                list.add(new SoftReference<Integer>(new Integer(i)));
            }
    
            /* Test */
            for (int i = 0; i < gc_count; ++i) {
                System.gc();
    
                try {
                    Thread.sleep(200);
                } catch (final InterruptedException e) {
                }
            }
    
            /* Check */
            int dead = 0;
            for (final SoftReference<Integer> ref : list) {
                if (ref.get() == null) {
                    ++dead;
                }
            }
            assertEquals(0, dead);
        }
    }
    

    The idea is that I make few runs of the same code increasing stress on SoftReferences each time (by running more GC passes).

    Results are pretty interesting: All runs pass just fine except for one!

    On Android 1.5 device:
    testSoftRefsAgainstGc_1()  FAILED!  AssertionFailedError: expected:0 but was:499
    testSoftRefsAgainstGc_2()  passed
    testSoftRefsAgainstGc_3()  passed
    testSoftRefsAgainstGc_4()  passed
    testSoftRefsAgainstGc_5()  passed
    testSoftRefsAgainstGc_6()  passed
    testSoftRefsAgainstGc_7()  passed
    
    
    On Android 1.6 device:
    testSoftRefsAgainstGc_1()  passed
    testSoftRefsAgainstGc_2()  FAILED!  AssertionFailedError: expected:0 but was:499
    testSoftRefsAgainstGc_3()  passed
    testSoftRefsAgainstGc_4()  passed
    testSoftRefsAgainstGc_5()  passed
    testSoftRefsAgainstGc_6()  passed
    testSoftRefsAgainstGc_7()  passed
    
    On Android 2.2 device:
    All pass.
    

    These test results are stable. I’ve tried many times and every time it is the same. So I believe it is indeed a bug in garbage collector.

    CONCLUSION

    So, what we learn out of this… Using SoftReferences in your code is pointless for Android 1.5-1.6 devices. For these devices you will not get the behavior you expect. I didn’t try for 2.1, however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use transactions in a project and it seams that they just
How can I use clip-path with Raphaël.js like this example . It seams that
It seems that runClasses() doesn't terminate the code being tested even after the test
It seems that NHibernate Linq doesn't understand grouped .Where method conditions. I got the
I have a problem when optimizing code, and it seams that the reason is
I'm having a problem with applicationShouldTerminate. What ever I do it seams that has
Pixel Perfect https://addons.mozilla.org/en-US/firefox/addon/pixel-perfect/ Is not working any more on FF6 and seams that they
It seams that @Route annotations are not working when doing functional tests with WebTestCase
It seams that we are not able to create a team within Organization but
It seams that it is unreasonable to put more than one test methods in

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.