1

I am creating a simple application for android, but i'm running to a weird problem. I have two activities, and a Dictionary IntentService. The Dictionary IntentService just reads a text file stored in the assets folder into a HashMap. Note that I have both activities in the Manifest.

This is all i'm doing :

    Intent intent = new Intent(this, AnActivity.class);
    startActivity(intent);
    Intent intentDict = new Intent(this, DictionaryService.class);
    startService(intentDict);

The first activity runs fine, it loads the second activity and shows it's UI on the nexus 7 emulator. But when it tries to run the 2nd Intent I get this OutOfMemory error:

05-02 23:29:48.507: I/dalvikvm(797):   at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 23:29:48.507: I/dalvikvm(797):   at android.os.Looper.loop(Looper.java:137)
05-02 23:29:48.507: I/dalvikvm(797):   at android.os.HandlerThread.run(HandlerThread.java:60)
05-02 23:29:48.517: W/dalvikvm(797): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
05-02 23:29:48.527: E/AndroidRuntime(797): FATAL EXCEPTION: IntentService[DictionaryService]
05-02 23:29:48.527: E/AndroidRuntime(797): java.lang.OutOfMemoryError
05-02 23:29:48.527: E/AndroidRuntime(797):  at java.util.HashMap.makeTable(HashMap.java:555)
05-02 23:29:48.527: E/AndroidRuntime(797):  at java.util.HashMap.doubleCapacity(HashMap.java:575)
05-02 23:29:48.527: E/AndroidRuntime(797):  at java.util.HashMap.put(HashMap.java:405)
05-02 23:29:48.527: E/AndroidRuntime(797):  at java.util.HashSet.add(HashSet.java:95)
05-02 23:29:48.527: E/AndroidRuntime(797):  at com.example.androidApp.Dictionary.readInData(Dictionary.java:73)
05-02 23:29:48.527: E/AndroidRuntime(797):  at com.example.androidApp.Dictionary.<init>(Dictionary.java:35)

Now the problem is that, when I do these individually they work fine. So if I run one of the two intents the application works normally. I've tried calling the DictionaryService intent from the newly loaded activity (AnActivity) but that results in the same error. Also with the emulator it has 1024 Mb of RAM allocated so it's not like it doesn't have enough memory. Any ideas?

Thanks

stackoverflow
  • 2,320
  • 3
  • 17
  • 21
Argilium
  • 492
  • 4
  • 8
  • 2
    "Also with the emulator it has 1024 Mb of RAM allocated so it's not like it doesn't have enough memory" -- that is how much RAM is in the emulated device. Your app will have *much* less heap space than that, somewhere in the 16-96MB range. – CommonsWare May 03 '13 at 00:15
  • How big is the file that you are reading? – Santa May 03 '13 at 00:50
  • @Santa The file is 2.5 MB, is that too big you reckon? If it is, should I find another way of implementing dictionary functionality? – Argilium May 03 '13 at 01:22

2 Answers2

0

If you are getting OutOfMemory exceptions passing around large intents, then you are most likely running out of Dalvik heap memory. Even though the machine in this case has 1024 MB of RAM, Android limits each individual Dalvik heap to 16-64 MB, based on the specific device on which you are running.

For more detail, read this informative answer: Detect application heap size in Android

Community
  • 1
  • 1
garamfalvi
  • 49
  • 3
0

If anyone has a similar problem, the issue was As @santa indicated in the comments before, the size of the file. So instead I split up the file into 3 smaller files and read them in consequently, and it worked

Argilium
  • 492
  • 4
  • 8