2

I want to get a clear picture of how creating a new object effects my RAM-usage.

My current game start off using 52036424 bytes of heap, however, after creating new objects (enemies, bullets, etc.) for about 1 minute, the heap is 67036424 bytes.

Does this mean I have a memory leak? Or is this just how Java works; creating new objects continously increases the required heap? (I do remove objects no longer in use).

Green_qaue
  • 3,561
  • 11
  • 47
  • 89
  • you cannot "remove" objects in Java; anyway, objects that become unreferenced, are periodically garbage collected. This usually happens when memory is required for other objects, and Android is quite good in scheduling this – Alex Salauyou Mar 17 '16 at 17:23
  • concluding: heap size can grow to make your app "feel comfortable" (i. e. create new objects quickly without need of often GC runs)--by itself this is not a symptom of memory leak. Memory leak will be indicated by sudden `OutOfMemoryError` – Alex Salauyou Mar 17 '16 at 17:28
  • Thank you, this helped me understand. – Green_qaue Mar 17 '16 at 17:34

3 Answers3

2

Well it depends on how garbage collection is working with your game. If you are creating a bunch of objects and they still have references to them, the GC will not clean them up.

It is possible that you are just using that much memory to run your game, but you also need to look at references to your bigger objects and see if you can destroy them earlier.

If you want to get crazy, you could do a heap dump and analyze it... jmap and jhat will get that done for you.

If it truly is a memory leak, there are tons of articles on how to debug that.

Also, you could just pump up your heap space.

chantheman
  • 5,256
  • 4
  • 23
  • 34
2

For Heap Space, you can extend your Xms and Xmx size of heap space.

export JVM_ARGS="-Xms1024m -Xmx1024m"

For PermGen Space, you can also extend PermSize and MaxPermSize of Permanent Generation space.

 JVM_ARGS="-XX:PermSize=512M -XX:MaxPermSize=1024m"

You can follow the tutorial

  1. http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html
  2. java.lang.OutOfMemoryError: PermGen space Exception with Eclipse (Spring and Hibernate Application)
Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
1

What is Heap space? - When a Java program is executed JVM gets some memory from RAM. JVM uses this memory for all its need and part of this memory is known as Java Heap Space. Whenever you create object using new operator or by any another means, object is allocated memory from Heap. When object is dead / garbage collected, memory goes back to the Heap.

You can change size of heap space by using JVM options -Xms and -Xmx. Xms denotes starting size of Heap while -Xmx denotes maximum size of Heap.

Java Heap space is divided amongst following three parts:

  • Young Gen [This space is made up of Eden space and Survivor spaces]
  • Old Gen
  • Perm Gen

Garbage Collection Process

  • All new objects created in your program gets first allocated to Young Gen's Eden Space. Once Eden space fills up, minor Garbage Collection is triggered. Live/Referenced objects are then moved to first survivor space. Unreferenced/dead objects are deleted and Eden space is cleared.
  • At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space.

  • At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.

  • After a minor GC, when aged objects reach a certain age threshold (8 in typical scenarios) they are promoted from young generation to old generation.

  • As minor GCs continue to occure objects will continue to be promoted to the old generation space.

  • Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.

JVM expands Heap in Java some where near to Maximum Heap Size specified by -Xmx and if there is no more memory left for creating new object in java heap, JVM throws java.lang.OutOfMemoryError and your application dies.

This is nicely explained here with graphical representation of Heap Space.

HIREN011
  • 549
  • 1
  • 7
  • 7