开发者

Reducing Memory Usage in Android Applications

开发者 https://www.devze.com 2023-03-28 21:50 出处:网络
I have a pretty simple game that is out on the Market right now. It is a simple text-based game, no 2d or 3d graphics involved. It uses properly-sized pngs for backgrounds. Other than that, it is pure

I have a pretty simple game that is out on the Market right now. It is a simple text-based game, no 2d or 3d graphics involved. It uses properly-sized pngs for backgrounds. Other than that, it is purely text based.

There is nothing cpu or graphic intensive about the game, but it averages about 25mb of memory u开发者_StackOverflowsage. In comparison, most apps that are even more cpu intensive average about 18mb.

In mobile applications, every megabyte counts, so what can I do to properly reduce memory usage in my app? I know this may be a vague question, but I will be glad to elaborate if at all needed.

Thank you


All components of the same application run in the same process and thread. The memory usage of service may be as big as the usage of whole application.

Try to run service in a separate process will fix this problem.

add android:process=":yourname" into AndroidManifest.xml like below:

<service android:name=".MyService" android:process=":yourname"> </service>

In my case, the memory usage of a simple service changes from 12MB to 3MB.


The first thing you should do is grab a memory dump from your app when the memory usage is high and use Eclipse Memory Analyzer to work out what is actually consuming the memory.

EDIT: These links may be helpful:

Android ==> Memory Analysing ==> Eclipse memory analyzer?

http://www.vogella.de/articles/EclipseMemoryAnalyser/article.html


If the content of your images includes any repeating sections you could split them in to several images and use them as tiles.

Nine patches could be used to do this easily, assuming your images adhere to the nine patch format.


To reduce memory you could possible have some of the graphics downloaded from a application server. Maybe some text documents, or png files you are using. You could actually have majority of your resources installed after the application is installed from a application server such Amazon s3

EDIT:

If you are using String buffer, think it might be to do with a performance optimisation of the StringBuffer toString() method.

The Sun javadoc says the following:

This method can be coded so as to create a new String object without allocating new memory to hold a copy of the character sequence. Instead, the string can share the memory used by the string buffer. Any subsequent operation that alters the content or capacity of the string buffer must then make a copy of the internal buffer at that time. This strategy is effective for reducing the amount of memory allocated by a string concatenation operation when it is implemented using a string buffer.

Because you maybe re-using the StringBuffer with the setLength(0) it might be keeping a reference to all the Strings it's created with toString().

Replace:

.setLength(0);

with:

"Your String buffer" = new StringBuffer();

and see if that resolves it. I don't think this will be any more overhead since in both cases you'll need to create a new char[] array, since in the first case the array is being used by the String created with toString().

Also, you should consider using a StringBuilder as they are preferred to StringBuffer.

Check here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号