14 Replies - 4480 Views - Last Post: 24 July 2013 - 11:05 AM

#1 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

App Memory ?

Post icon  Posted 20 July 2013 - 05:22 PM

Hi guys. Im kind of new to android and I just released another app on google play a few weeks ago called Health Genie swipe. This app used about 23Mb of memory which is ridiculous i know. It has about 50 images across roughly 45 Activities. I was wondering what kinds of things could i do in the development process of future apps that could cut down on the memory usage of the app and still use lots of images. By the way please go download my app. Thanks =D

Is This A Good Question/Topic? 2
  • +

Replies To: App Memory ?

#2 YasuoDancez   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: App Memory ?

Posted 20 July 2013 - 07:04 PM

Recycling objects such as bitmaps, Views etc is very important, learn about that. In the more recent API Levels, recycling of Bitmaps happens automatically.
In the Android Guides or Training, there are great instructions that show developers how to display bitmaps efficiently, and they show you how to create memory caches with LRU

This video lecture talks about how memory works in Android. Be careful of memory leaks, it can happen even with Garbage Collection (GC).
Was This Post Helpful? 3
  • +
  • -

#3 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

Re: App Memory ?

Posted 20 July 2013 - 07:10 PM

Thanks a million. =D
Was This Post Helpful? 0
  • +
  • -

#4 YasuoDancez   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: App Memory ?

Posted 20 July 2013 - 07:23 PM

You're welcome :bananaman:
Was This Post Helpful? 1
  • +
  • -

#5 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: App Memory ?

Posted 20 July 2013 - 07:30 PM

With an app of this type, you should have no problem loading a bitmap into its view only when needed. In other words, you load the activity and the view loads its drawable. in onpause() you set the drawable to null, then in onresume() you load it. It will be more than fast enough reading from the device's file system, even the sd card.

I get a feeling that you are:

Bitmap firstImage = BitmapFactory.......



and loading all the images when the app is first run, correct?
Was This Post Helpful? 2
  • +
  • -

#6 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

Re: App Memory ?

Posted 21 July 2013 - 09:37 AM

Well i was setting all of the images in the XML as images views. I didn't try the way that you just specified yet but i'm about to. Thanks =)
Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: App Memory ?

Posted 21 July 2013 - 03:34 PM

23Mb =~ 2.8 MB, thats not very much space at all.
Was This Post Helpful? 0
  • +
  • -

#8 YasuoDancez   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: App Memory ?

Posted 21 July 2013 - 11:53 PM

^ Yeah I noticed that also. Mb = Megabits, but I think he meant 23 MB Megabytes.
If your app is using 1 process and 1 service, that is still not too bad. The Google+ app uses about 34MB with 1 process and 1 service.
Facebook app uses 65MB with 1 process and 3 services.
Was This Post Helpful? 0
  • +
  • -

#9 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

Re: App Memory ?

Posted 22 July 2013 - 09:40 AM

That was a typo. I really meant MB sorry. But still i could get my apps memory to a lot less then that with better programming practices. The game logo quiz has over 1000 bitmap images in it and its only 12MB.
Was This Post Helpful? 0
  • +
  • -

#10 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

Re: App Memory ?

Posted 23 July 2013 - 12:43 PM

View Postfarrell2k, on 20 July 2013 - 07:30 PM, said:

With an app of this type, you should have no problem loading a bitmap into its view only when needed. In other words, you load the activity and the view loads its drawable. in onpause() you set the drawable to null, then in onresume() you load it. It will be more than fast enough reading from the device's file system, even the sd card.

I get a feeling that you are:

Bitmap firstImage = BitmapFactory.......



and loading all the images when the app is first run, correct?



ferrell2k I tried to do what you said and set the image to null in the onpause() and then have it load it in the onresume() but when I run the code the emulator crashes.

package com.downs.bitmapmemory;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity1 extends Activity {

Resources res = getResources();
Drawable first_pic = res.getDrawable(R.drawable.workout_comic_1);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen1);
		
		
		Button btn_1 = (Button)findViewById(R.id.button_1);
		btn_1.setonclickListener(new View.onclickListener(){
			
			@Override
			public void onclick(View view){
			
			
				startActivity(new Intent(Activity1.this, Activity2.class));
				
			}
		});
	
	
	
	
	}

	@Override
	protected void onpause() {
		super.onpause();
		
		//SET DRAWABLE REFERENCE TO NULL
	
		first_pic = null;
		
	}

	@Override
	protected void onresume() {
		super.onresume();
		
		//LOAD THE DRAWABLE IMAGE 
		
		
		 first_pic = res.getDrawable(R.drawable.workout_comic_1);
		
	}

	
	
	
}



Was This Post Helpful? 0
  • +
  • -

#11 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

Re: App Memory ?

Posted 23 July 2013 - 12:57 PM

Would I have to set the UI in java code ?

This post has been edited by macosxnerd101: 23 July 2013 - 01:06 PM
Reason for edit:: Please avoid quoting large posts

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12657
  • View blog
  • Posts: 45,831
  • Joined: 27-December 08

Re: App Memory ?

Posted 23 July 2013 - 01:06 PM

Quote

ferrell2k I tried to do what you said and set the image to null in the onpause() and then have it load it in the onresume() but when I run the code the emulator crashes.

Post your stack trace.
Was This Post Helpful? 0
  • +
  • -

#13 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

Re: App Memory ?

Posted 23 July 2013 - 01:43 PM

View Postmacosxnerd101, on 23 July 2013 - 01:06 PM, said:

Quote

ferrell2k I tried to do what you said and set the image to null in the onpause() and then have it load it in the onresume() but when I run the code the emulator crashes.

Post your stack trace.


From LogCat
07-23 22:34:24.159: E/AndroidRuntime(359): FATAL EXCEPTION: main
07-23 22:34:24.159: E/AndroidRuntime(359): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.downs.bitmapmemory/com.downs.bitmapmemory.Activity1}: java.lang.NullPointerException
07-23 22:34:24.159: E/AndroidRuntime(359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.os.Looper.loop(Looper.java:123)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-23 22:34:24.159: E/AndroidRuntime(359): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 22:34:24.159: E/AndroidRuntime(359): at java.lang.reflect.Method.invoke(Method.java:521)
07-23 22:34:24.159: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-23 22:34:24.159: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-23 22:34:24.159: E/AndroidRuntime(359): at dalvik.system.NativeStart.main(Native Method)
07-23 22:34:24.159: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
07-23 22:34:24.159: E/AndroidRuntime(359): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
07-23 22:34:24.159: E/AndroidRuntime(359): at com.downs.bitmapmemory.Activity1.<init>(Activity1.java:13)
07-23 22:34:24.159: E/AndroidRuntime(359): at java.lang.Class.newInstanceImpl(Native Method)
07-23 22:34:24.159: E/AndroidRuntime(359): at java.lang.Class.newInstance(Class.java:1429)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-23 22:34:24.159: E/AndroidRuntime(359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-23 22:34:24.159: E/AndroidRuntime(359): ... 11 more
07-23 22:36:59.734: E/AndroidRuntime(388): FATAL EXCEPTION: main
07-23 22:36:59.734: E/AndroidRuntime(388): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.downs.bitmapmemory/com.downs.bitmapmemory.Activity1}: java.lang.NullPointerException
07-23 22:36:59.734: E/AndroidRuntime(388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.os.Looper.loop(Looper.java:123)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-23 22:36:59.734: E/AndroidRuntime(388): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 22:36:59.734: E/AndroidRuntime(388): at java.lang.reflect.Method.invoke(Method.java:521)
07-23 22:36:59.734: E/AndroidRuntime(388): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-23 22:36:59.734: E/AndroidRuntime(388): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-23 22:36:59.734: E/AndroidRuntime(388): at dalvik.system.NativeStart.main(Native Method)
07-23 22:36:59.734: E/AndroidRuntime(388): Caused by: java.lang.NullPointerException
07-23 22:36:59.734: E/AndroidRuntime(388): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
07-23 22:36:59.734: E/AndroidRuntime(388): at com.downs.bitmapmemory.Activity1.<init>(Activity1.java:13)
07-23 22:36:59.734: E/AndroidRuntime(388): at java.lang.Class.newInstanceImpl(Native Method)
07-23 22:36:59.734: E/AndroidRuntime(388): at java.lang.Class.newInstance(Class.java:1429)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-23 22:36:59.734: E/AndroidRuntime(388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-23 22:36:59.734: E/AndroidRuntime(388): ... 11 more
Was This Post Helpful? 0
  • +
  • -

#14 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: App Memory ?

Posted 23 July 2013 - 06:07 PM

Can you follow this code? Note that the editor on this site has improperly named onpause() and onresume().

public class MainActivity extends Activity {
	private ImageView iv;
	private Bitmap ivImage;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		iv = new ImageView(this);
		setContentView(iv);
	}
	
	public void onpause() {
		super.onpause();
		iv.setImageBitmap(null);
		ivImage = null;
	}
	
	public void onresume() {
		super.onresume();
		ivImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		iv.setImageBitmap(ivImage);
	}
}


This post has been edited by farrell2k: 23 July 2013 - 06:12 PM

Was This Post Helpful? 1
  • +
  • -

#15 XxLegendxX   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 10-April 12

Re: App Memory ?

Posted 24 July 2013 - 11:05 AM

View Postfarrell2k, on 23 July 2013 - 06:07 PM, said:

Can you follow this code? Note that the editor on this site has improperly named onpause() and onresume().

public class MainActivity extends Activity {
	private ImageView iv;
	private Bitmap ivImage;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		iv = new ImageView(this);
		setContentView(iv);
	}
	
	public void onpause() {
		super.onpause();
		iv.setImageBitmap(null);
		ivImage = null;
	}
	
	public void onresume() {
		super.onresume();
		ivImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		iv.setImageBitmap(ivImage);
	}
}




Thank you so much farrell2k this worked beautifully =DD
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1