1

I am returning to Java after a 5 year break from it. If I remember correctly the Garbage collector would kick in and collect the 'new' memory after subsequent calls to setListAdapterStrings(String [] data)? To be more general does anyone have a preferred set of documentation they like to use when it comes to producing memory leaks while using the JVM?

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class MainActivity extends ListActivity {
    private ListAdapter mListAdapter;

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

        createAdapter();
        setListAdapter(mListAdapter);
    }

    protected void createAdapter() {
        // Create some mock data
        String[] testValues = new String[] {
                "Hello",
                "Welcome",
                "The",
                "Java",
                "Test",
                "World",
        }; 
        setListAdapterStrings(testValues);
    }

    public void setListAdapterStrings(String [] data) {
        mListAdapter = new ArrayAdapter<String>(
                this, 
                android.R.layout.simple_list_item_1, 
                data);
    }
}
7
  • 2
    In theory, Java doesn't "leak". However, Android Java is not Java, and several system-related objects (notably images) can "leak" and need to be freed through one of several mechanisms. Been away from Android for about 3 years now, though, so I don't recall the details. Commented Jul 9, 2013 at 20:26
  • @SotiriosDelimanolis Read the question at the top. I am asking if I make subsequent calls to setListAdapterStrings(String [] data) will produce a memory leak when re-newing mListAdapter. Commented Jul 9, 2013 at 20:31
  • @MatthewHoggan It would not. Commented Jul 9, 2013 at 20:37
  • @SotiriosDelimanolis Thanks, I have been doing C++ development for the last 4 years and am a schizophrenic when it comes to memory leaks. I know Java can have "memory leaks" when you put to much on the heap and keep it in scope so that the Garbage collector cant clean up. Just looking for any other cases I need to be aware of. Commented Jul 9, 2013 at 20:42
  • @MatthewHoggan It's great to be aware of that. More people should alternate between languages that do and don't require memory management. With android, check for what Hot Licks has said. Commented Jul 9, 2013 at 20:44

1 Answer 1

3

No, that is not a memory leak. Java does not require explicit free-ing of memory. The now-unreferenceable ListAdapters will be collected in the future by the Garbage Collector.

Typically, memory is leaked in Java two ways:

  1. Unintentionally retaining references too long
  2. Not properly disposing of lower-level resources (database connections, sockets, etc).

In the first case, "leak" is really a misnomer. It's still reachable, which is why it's not collected, so it can't be said to have leaked, but it's probably not used any more.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the clear description. Moving from C++ back to Java can be a dangerous thing.
Not dangerous ... just simpler. Garbage collection simplifies many of the things you'd otherwise have to code for in C++. When a reference falls out of scope in C++, as long as another reference to the same object is not retained elsewhere, then the object becomes automagically eligible for collection without any further input from you. The only situations in which memory leaks are likely in Java are noted above and also when you -effectively- begin to manage memory yourself (such as when you manage a cache size in your code).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.