Wednesday, November 13, 2013

Run Android project generated by Maven archetype:generate from IDE by Maven commands

When you create an Android project by Maven e.g. mvn archetype:generate your new Project will be presented in IDE as a Java project, not the Android's one. So you can't run it from IDE as an Android project "out of the box". You should use Maven commands instead:

mvm android:deploy
mvm android:run

By default Maven will deploy your apk on all available deviсes including USB's ones and emulator. So if you want to use only USB devices change related setting in pom.xml of the project.

Understanding Memory Management

The summary from this link.

HEAP is shared among all THREADS.
The heap is the RUN-TIME DATA AREA from which memory for all class instances and arrays is allocated.

OBJECTS are kept in HEAP memory.

When HEAP is full the GARBAGE is COLLECTED.

Java METHODS, THREADS, NATIVE HANLES are allocated in other memory (methods_area, stack).

TWO AREAS OR GENERATIONS OF HEAP
HEAP is divided on two areas (generations):

YOUNG_SPACE (NURSERY) for a new OBJECTS.

When NURSERY is full garbage collector runs YOUNG_COLLECTION and moves some obj. to OLD_SPACE (all threads are stopped. it is called GENERATIOANL or PARALLEL GarbCol-on where the work is done in parallel using all available CPUs.)
When OLD_SPACE is full garbage is collected by process named OLD_COLLECTION

In NURSERY is a KEEP_AREA for a newly added objects that lives there until the next YOUNG_COLLECTION. This prevents objects from being promoted (moved) just because they were allocated right before a young collection started.

JVM distinguishes SMALL and LARGE OBJECTS. (depends of JVM version,  HEAP size, GC... )
LARGE size ~ 2 - 128 kB. See the docs for -XXtlaSize and -XXlargeObjectLimit

LARGE OBJECTS allocated in TLA (thread local area) that is reserved in HEAP.



GARBAGE COLLECTION MODELS
MARK and SWEEP models.
MARK - marks up all alive objects, SWEEP writes a gaps among'em in a free list for a new objects.

Its improved versions are CONCURRENT concurrent garbage collection or PARALLEL mark and sweep.

GARBAGE COLLECTION MODES
DYNAMIC automatically selects a garbage collection strategy to use:
throughput (default mode), pausetime, deterministic.

STATIC mode uses this major strategies:
singlepar, which is a single-generational parallel garbage collector (same as parallel)
genpar, which is a two-generational parallel garbage collector
singlecon, which is a single-generational mostly concurrent garbage collector
gencon, which is a two-generational mostly concurrent garbage collector

COMPACTION
To reduce fragmentation, the JRockit JVM compacts a part of the heap at every garbage collection JVM uses COMPACTION. Compaction is performed at the beginning of or during the SWEEP phase and while all Java THREADS are PAUSED.

TWO COMPACTION METHODS
EXTERNAL
moves the objects within the compaction area to free positions outside the compaction area and as far down in the heap as possible. 

INTERNAL
moves the objects within the compaction area as far down in the compaction area as possible, thus moving them closer together.

SLIDING WINDOW SCHEMES
Each sliding window moves a notch up or down in the heap at each garbage collection, until it reaches the other end of the heap or meets a sliding window that moves in the opposite direction, and starts over again. Thus the whole heap is eventually traversed by compaction over and over again.

COMPACTION AREA SIZING
Thus the compaction area will be smaller in parts of the heap where the object density is high or where the amount of references to the objects within the area is high. Typically the object density is higher near the bottom of the heap than at the top of the heap, except at the very top where the latest allocated objects are found. Thus the compaction areas are usually smaller near the bottom of the heap than in the top half of the heap.