Code reviews

Anyone want to do some paid code reviews ?

I have a memory leak somewhere and no idea how to track it down.

Code reviews for it would be a last resort. Did you already try to do use hprof?
Do you have the issue reproducible or do you know about it just from ‘Run out of VM memory’ crashes?

Run out of men crashes all over the place… and never ever on my hardware :frowning:

In case you haven’t already thought of it, you might be able to focus your efforts by inspecting the diffs between your code from this release and the code from the previous release. Good luck!

Have you tried memory pfofiling to see whats using all your heap memory? http://developer.android.com/tools/debugging/ddms.html

Any luck tracking down the memory leak with DDMS Mind?

Nope… got no clue what I am looking for in DDMS. Could be a dozen leaks for all I know. That is why I was hoping to be able to contract someone to sit with me via screen share and fix the problem.

I think the problem is most people here are indie developers that may not have much more knowledge on memory leaks than yourself. And even if they may be able to find it, they would not feel comfortable getting paid to help you when they don’t know if they could actually find it or not. Especially since it doesn’t happen on all hardware.

At least that is how I feel.

Well, I started developing games to stop freelancing for boring jobs like this. :slight_smile: Finding memory leaks is hard work and it’s best done by yourself - someone new would have to dive into your code to properly understand it and it costs a lot of time…

Well, I think the point was that I was willing to pay for time… with the objective to find memory leaks. I can start up an activity, then back out of it to the previous activity and see the memory consumed has gone up even after a GC. However, I don’t know enough to track it from just that.

Have you tried simplifying the code by uncommenting some things? This is always the first thing I do. You are using Java right? Read about when memory leaks occur in Java - it’s a little different than in languages like C/C++.

This might help
http://android-developers.blogspot.com.au/2011/03/memory-analysis-for-android.html?m=1

Read that… again and again… still no luck figuring this out though.

On the upside, I wrote a small app that alternates between two activities and just shows an advert. No memory leak from Inner-active. So it seems the bug is mine :frowning:

Do you use cursors?

I don’t do any database stuff.

However, I do have a bunch of activities that I transition between and destroy the old activity.

Are you using threads and Handlers to send Messages between them and are you using postDelayed to do it?

Got any static non-primitives anywhere that might be holding references to other classes so they can’t be garbage collected?

Are you using TypedArrays anywhere and not recycling them?

I use a lot of ArrayList<String> and other ArrayList<obj>. Game timers are done with a handler and postDelayed. I also create arrays for ListView adaptors.

I assumed all this stuff got GC’d ?

The ArrayLists do as long as they aren’t static. Postdelayed and Handlers can have issues which you may or may not be at risk of encountering.

Example to explain static vs non-static behaviour:

public class Example extends Activity{
	private static ArrayList<String> staticArrayList = new ArrayList<String>();
	private ArrayList<String> nonStaticArrayList = new ArrayList<String>();
}

When an instance of Example is eligible for garbage collection the instance of Example will be GC’d along with the instance member nonStaticArrayList.

However staticArrayList is not specific to that instance of the class and it will not be garbage collected. In fact, on Android I’m fairly sure they only time static variables are GC’d is when the apps process is ended. They can be gc’d if: The class is unloaded or the JVM shuts down or the process dies but only process death is likely to apply.

If staticArrayList was an ArrayList<Entities> and Entities contained references to the Activity or or any other classes then those Entities +everything they reference couldn’t be collected and you’d be “leaking” everything they referenced when you moved to a new activity.

This applies to all static variables and not just ArrayLists obviously.

Handlers and Messages and postDelayed

Using Messages with postDelayed to a custom Handler can cause leaks depending on the length of the delays and how it’s handled. If you’ve run Android Lint over your code it’ll probably already have given you a warning if your using a Handler in a way that could leak. There is a Q&A on the relevant warning on stackoverflow http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler

The basic problem is if you use a long delay with postDelayed you can leak your whole Activity because; The Looper for the thread has a list of Messages, those Messages have references to the Handler they are going to and if the Handler is an inner class then it has a reference to it’s outer class’ instance (which is usually an Activity). In this way the Activity and everything that goes with it can be leaked.

These leaks only exist until the Messages have all been processed (at that time the references to the Handler are all gone and it and anything it references can become eligible for GC, but you could run out of memory if it takes a while and you are using a lot of memory in the current Activity and the next Activity you move to.

Of course you may well have some other totally different problem.

Yeah… I don’t use static anything (except in the Application context) and handlers are only used to post stuff from an Async thread to the UI.

Are you sure you have a memory leak?
You can use ddms to check what objects reserved memory.
You can check the list and see if you see objects that should not be there.