Author Topic: Shouldn't MemoryHelper.compact() Free All My RAM?  (Read 991 times)

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Shouldn't MemoryHelper.compact() Free All My RAM?
« on: December 06, 2013, 07:52:27 pm »
I wrote a little endless runner, which I tested with textures 512x512 in size, on both the Nexus 10 tablet and the Galaxy S4. It ran fine. On the Galaxy Tab 2, for which the game was actually written as that is my son's tablet, it doesn't run at all. I've even reduced the textures to 256x256. After loading all the textures I call MemoryHelper.compact(). But before even the first chunk of city is loaded (and during its loading process), the game crashes with an OutOfMemoryError. I would think that the model itself isn't so complex that it would crash for simply trying to load this chunk (it has a total of 36k triangles). Is it possible that compact isn't doing its job (at least not right away)? Any suggestions?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11744
    • View Profile
    • http://www.jpct.net
Re: Shouldn't MemoryHelper.compact() Free All My RAM?
« Reply #1 on: December 07, 2013, 08:57:03 am »
compact() doesn't do more than calling some combination of runFinalization and gc which forces the vm to free unused memory. It used to help on older devices. You should check how much memory the vm on this tab provides. Maybe its Android version already supports the large memory setting in the manifest. That exactly are you loading (3ds, obj, serialized...)?

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Shouldn't MemoryHelper.compact() Free All My RAM?
« Reply #2 on: December 07, 2013, 05:24:10 pm »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11744
    • View Profile
    • http://www.jpct.net
Re: Shouldn't MemoryHelper.compact() Free All My RAM?
« Reply #3 on: December 07, 2013, 06:01:29 pm »
Yes, that's the usual blahblah about System.gc();. Let's call it a strong suggestion. One that you usually can't resist...
Apart from that, this isn't needed except on some 1.x based devices that had issues with collecting unused native memory in time.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Shouldn't MemoryHelper.compact() Free All My RAM?
« Reply #4 on: December 07, 2013, 07:24:27 pm »
It's a serialized object. Interesting about the large memory setting. I'll look into it.

Offline AGP

  • quad
  • ******
  • Posts: 1515
    • View Profile
Re: Shouldn't MemoryHelper.compact() Free All My RAM?
« Reply #5 on: December 07, 2013, 07:39:25 pm »
android:largeHeap="true" did it for me, thanks a lot. Is the reason that the S4 and Nexus 10 not need this that their normal heap is as large (or nearly) as the Tab 2's large heap?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11744
    • View Profile
    • http://www.jpct.net
Re: Shouldn't MemoryHelper.compact() Free All My RAM?
« Reply #6 on: December 09, 2013, 08:09:35 am »
Heap differs from Android version to Android version and from device to device. As a rule of thumb, one can say that it increaes with each Android version but the vendor is free to adjust it if he feels that this is needed.