Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

Logic behind memory leak in android

Normally in c++ what memory leak is , If we have alloted an object like

Obj c = new Obj();

then if we do

c = b; (example)

we lose the pointer to the object c that's the memory leak.

Question:

But in android Garbage collector collects object when there is no pointers pointing to them. So why there is memory leak even after that ?

Update

All the answers points to holding reference to unused objects is causing memory leak. Thats right. But is that the only cause of memory leak. Holding pointers will be released when activity is finished unless it is static. There are bitmaps and other memory hunger objects , don't they cause any problems in this

Upvotes: 4

Views: 290

Answers (5)

Nathua
Nathua

Reputation: 8826

I think we need to define "Memory Leak" in the first place. Memory leak is something you don't need anymore but it still in the memory and each time a new object is created, a new place will be allocated in the memory. The application will hold more and more memory in time.

private static Drawable background;

@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);

  TextView label = new TextView(this);

  background = getDrawable(R.drawable.large_bitmap);

  label.setBackgroundDrawable(background);

  setContentView(label);
}

Above example, TextView get Activity as reference, which is a link between

TextView and Activity

now, then there is a static variable

background

which holds the background drawable, static variable will be alive until the application is destroyed or finished. Imagine that you want to destroy the activity, when you destroy static variable will still hold a link to activity, because of that garbage collector won't be able to collect it.

You can have a look at here about more.

Upvotes: 1

nKn
nKn

Reputation: 13761

The answer is partially in your question: Precisely because those references are not freed. Suppose a situation where you have an instance of a class, you've ended working in it but for certain circumstances, the instance keeps there in a state it's not freed. Additionally to Garbage Collector unability to free it, if not controlled, the amount of memory may even increase if it's not handled.

There is one additional thing too. You may have a code that looks correct and well written, but when you instantiate some native libraries, that means you're referencing an hierarchy of classes. If you're not aware of what are you doing, you may precisely handle incorrectly some references and lead to memory leaks. One very popular example is keeping a Context reference into your class. The context instance is never freed and a reason of huge memory leaks.

There are solutions for this, additionally to the obvious (free objects when you're done, etc.). In Java there are SoftReferences, WeakReferences, and other. This objects are containers that tell the Garbage Collector that they have preference on being freed once unused or there are no other references pointing to them. So you're helping the GC to know what should be freed. They're dangerous at a certain point in Android environments, as apps are limited too 16MB of heap, so a WeakReference might be collected too fast. It's necessary to check whether the object still exists.

Upvotes: 1

galex
galex

Reputation: 3319

There might be a leak when a Context (an Activity, Service, etc.) is retained by any helper class that would have a reference to it.

Illustration: instead of this:

public class Helper {

    private static Context mContext;

    public Helper(Context context){

        mContext = context;
    }

    public static void methodDoesSomething(){

        ...
    }
}

Use the context without retaining it by passing it as a parameter:

public class Helper {

    public static void methodDoesSomething(Context context){
        ...
    }
}

Because Android will want at some point to destroy an Activity for instance, and an object has a reference to it, The Garbage collector can not remove that Activity thus we have a memory leak.

Upvotes: 1

laalto
laalto

Reputation: 152817

In garbage collected runtimes, a memory leak means that an object cannot be collected even if it is no longer used. For example, a reference is held to an object but the reference is no longer used for anything.

Casually, the term memory leak is used to refer to situations where these incollectible objects keep on accumulating, increasing the allocated heap size and eventually leading to OOM.

Upvotes: 1

Umer Farooq
Umer Farooq

Reputation: 7486

In Android/Java memory leak occurs

  • when you are keeping the reference of an object/instance even after it is no longer required.

  • when you keep open a file stream, when you are done with it.

  • Unclosed connections

There are other reasons for memory leak as well, but these are the most common ones.

Upvotes: 1

Related Questions