Friday, 29 March 2013

Android Command Line Hacks

Android Command Line Hacks

[1] Update Project

Create a new project from existing code,

ANDROID_HOME=/home/prayag/android-sdks/
prayag@prayag:/BirthdayForestGap$ $ANDROID_HOME/tools/android update project --path ../BirthdayForestGap/
Updated local.properties
No project name specified, using Activity name 'BirthdayForestViewController'.
If you wish to change it, edit the first line of build.xml.
Added file ../BirthdayForestGap/build.xml
Updated file ../BirthdayForestGap/proguard-project.txt
It seems that there are sub-projects. If you want to update them
please use the --subprojects parameter.

If you are using android studio in mac, the location of sdk is

λ ls -l /Users/prayagupd/Library/Android/sdk/
total 0
drwxr-xr-x   7 prayagupd  184630988  224 Sep  8 09:08 build-tools
drwxr-xr-x  17 prayagupd  184630988  544 Jan 10  2018 emulator
drwxr-xr-x   6 prayagupd  184630988  192 Jan 10  2018 extras
drwxr-xr-x   3 prayagupd  184630988   96 Jan 10  2018 fonts
drwxr-xr-x   4 prayagupd  184630988  128 Jan 10  2018 licenses
drwxr-xr-x   3 prayagupd  184630988   96 Jan 10  2018 patcher
drwxr-xr-x  19 prayagupd  184630988  608 Jan 21  2019 platform-tools
drwxr-xr-x   7 prayagupd  184630988  224 Nov  9 10:29 platforms
drwxr-xr-x  26 prayagupd  184630988  832 Jun 16  2019 skins
drwxr-xr-x   5 prayagupd  184630988  160 Jun 17  2019 sources
drwxr-xr-x   3 prayagupd  184630988   96 Jan 10  2018 system-images
drwxr-xr-x  14 prayagupd  184630988  448 Jan 10  2018 tools

[2] ASM - Android SDK Manager
prayag@prayag: /BirthdayForestGap$ $ANDROID_HOME/tools/android sdk




prayag@prayag: /BirthdayForestGap$ ps aux | grep android
prayag   13409  2.6  1.9 473456 74852 pts/3    Sl+  22:16   0:20 java
-Xmx256M
-Dcom.android.sdkmanager.toolsdir=/home/prayag/android-sdks/tools
-classpath /home/prayag/android-sdks/tools/lib/sdkmanager.jar:/home/prayag/android-sdks/tools/lib/swtmenubar.jar:/home/prayag/android-sdks/tools/lib/x86/swt.jar com.android.sdkmanager.Main
prayag   13749  0.0  0.0   4400   836 pts/1    S+   22:29   0:00 grep --color=auto android


You can list sdks
ANDROID_HOME=/usr/local/android-studio/sdk
$ $ANDROID_HOME/tools/android list sdk
Refresh Sources:
  Fetching https://dl-ssl.google.com/android/repository/addons_list-2.xml
  Validate XML
  Parse XML
  Fetched Add-ons List successfully
  Refresh Sources
  Fetching URL: https://dl-ssl.google.com/android/repository/repository-8.xml
  Validate XML: https://dl-ssl.google.com/android/repository/repository-8.xml
  Parse XML:    https://dl-ssl.google.com/android/repository/repository-8.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/addon.xml
  Validate XML: https://dl-ssl.google.com/android/repository/addon.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/addon-6.xml
  Validate XML: https://dl-ssl.google.com/android/repository/addon-6.xml
  Parse XML:    https://dl-ssl.google.com/android/repository/addon-6.xml
  Fetching URL: https://dl-ssl.google.com/glass/gdk/addon.xml
  Validate XML: https://dl-ssl.google.com/glass/gdk/addon.xml
  Parse XML:    https://dl-ssl.google.com/glass/gdk/addon.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/extras/intel/addon.xml
  Validate XML: https://dl-ssl.google.com/android/repository/extras/intel/addon.xml
  Parse XML:    https://dl-ssl.google.com/android/repository/extras/intel/addon.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
Refresh Sources:
  Fetching URL: https://dl-ssl.google.com/android/repository/addon.xml
  Validate XML: https://dl-ssl.google.com/android/repository/addon.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
Refresh Sources:
  Fetching URL: https://dl-ssl.google.com/android/repository/addon.xml
  Validate XML: https://dl-ssl.google.com/android/repository/addon.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Fetching URL: https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
Packages available for installation or update: 15
   1- Android SDK Tools, revision 23
   2- Android SDK Platform-tools, revision 20
   3- Documentation for Android SDK, API 19, revision 2
   4- SDK Platform Android 4.4.2, API 19, revision 3
   5- Samples for SDK API 19, revision 5
   6- Samples for SDK API 18, revision 1
   7- Samples for SDK API 17, revision 1
   8- Samples for SDK API 16, revision 1
   9- Samples for SDK API 15, revision 2
  10- Samples for SDK API 14, revision 2
  11- Samples for SDK API 13, revision 1
  12- Samples for SDK API 12, revision 1
  13- Samples for SDK API 11, revision 1
  14- Samples for SDK API 10, revision 1
  15- Glass Development Kit Preview, Android API 19, revision 8



[3] AVD(Virtual Device) Manager
prayag@prayag: /BirthdayForestGap$ $ANDROID_HOME/tools/android avd




$ANDROID_HOME//tools/android list targets | grep android-23

[3.1] Start any of the AVDs from the list already created. Same thing can be achieved through 
prayag@prayag: /BirthdayForestGap$ $ANDROID_HOME/tools/emulator -avd AVD -netspeed full -netdelay none
emulator: emulator window was out of view and was recentered



[3.2] List AVD confs
$ ls -l ~/.android/avd/
total 40
drwxrwxr-x 2 prayag prayag 4096 May 14  2013 avd.avd
-rw-rw-r-- 1 prayag prayag   72 Jul 20  2012 avd.ini
drwxrwxr-x 2 prayag prayag 4096 Jun 23  2013 FroyoVD.avd
-rw-rw-r-- 1 prayag prayag   60 Apr 28  2013 FroyoVD.ini
drwxrwxr-x 2 prayag prayag 4096 Sep 28  2012 Google31TV.avd
-rw-rw-r-- 1 prayag prayag   84 Aug  9  2012 Google31TV.ini
drwxrwxr-x 2 prayag prayag 4096 Apr 21  2013 HC32.avd
-rw-rw-r-- 1 prayag prayag   58 Sep 11  2012 HC32.ini
drwxrwxr-x 2 prayag prayag 4096 Dec 31 23:50 Kitkat.avd
-rw-rw-r-- 1 prayag prayag  112 Dec 25 23:59 Kitkat.ini

[4] ant build

REFERENCES
[1] How do I launch the Android emulator from the command line?,
http://stackoverflow.com/a/4974695/432903

[2] http://developer.android.com/tools/building/building-cmdline.html

Tuesday, 19 March 2013

Evaluating Lisp Expressions in Emacs *ielm* * mode


STEP 1 : run M-x ielm to open up an elisp interpreter. (M is [alt] key in macOs)

NOTE:
ielm mode ( = Inferior Emacs Lisp mode) is a major mode for interactively evaluating emacs lisp expressions. More on this mode can be found by ELISP> (describe-mode)




Lisp expressions are either atoms or applications of functions.

atoms are simplest objects in e-lisp
ELISP> 28
28 (#o34, #x1c, ?\C-\\)

functions
STEP 2.1 : def a no-args function and invoke it

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (defun get-ipaddress() 
             "192.168.2.28")
get-ipaddress
ELISP> (get-ipaddress)
"192.168.2.28"
ELISP> 




ELISP> (defun sumUpto5() (+ 1 2 3 4 5))
sumUpto5
ELISP> (sumUpto5)


15 (#o17, #xf, ?\C-o)


STEP 2.2 : defn a function that takes an argument

ELISP> (defun releasePackage(packageId) 
         "releases package" 
            (concat "releasing package " packageId))
releasePackage

ELISP> (releasePackage "PACKAGE001")
"releasing package PACKAGE001"


STEP3 : variables/ data-structures

ELISP> (defun rock-bands() 
        (list "porcupine tree" "pink floyd" "king crimson"))
rock-bands

ELISP> (rock-bands)
("porcupine tree" "pink floyd" "king crimson")

ELISP> (car (rock-bands))
"porcupine tree"

ELISP> (cdr (rock-bands))

("pink floyd" "king crimson")


;; variables

ELISP> (setq metal-bands-global-list '("bring me the horizon" "parkway drive" "being as an ocean"))
("bring me the horizon" "parkway drive" "being as an ocean")

ELISP> metal-bands-global-list

("bring me the horizon" "parkway drive" "being as an ocean")

;; local
ELISP> (let ((number-of-plays-in-jan 100)
             (number-of-plays-in-feb 200))
         (+ number-of-plays-in-jan number-of-plays-in-feb))

300 (#o454, #x12c, ?Ĭ)

STEP4 : Conditionals
ELISP> (defun artist-growth (jan-count feb-count)
           (if (> jan-count feb-count)
            "your listeners are decreasing"
            "your listeners are increasing"))
artist-growth

ELISP> (artist-growth 10 15)

"your listeners are increasing"

PART2 ;; TODO
Recursion, Anonymous funs Higher-order functions


Highly Recommended References
Emergency Elisp, January 24, 2008, http://steve-yegge.blogspot.com/2008/01/emergency-elisp.html

References 
[1] How to invoke an interactive elisp interpreter in Emacs?, http://stackoverflow.com/a/146251/432903

[2] Emacs Lisp for Hackers: Hello World, Joel McCracken,  http://joelmccracken.github.com/entries/emacs-lisp-for-hackers-next/

[3] first emacs lisp script: hello world, http://lists.gnu.org/archive/html/help-gnu-emacs/2007-12/msg00163.html

[4] GNU Emacs Lisp Reference Manual

[5] http://harryrschwartz.com/2014/04/08/an-introduction-to-emacs-lisp.html

[6] https://www.emacswiki.org/emacs/UnitTesting

Wednesday, 13 March 2013

Day 1 with erlang

Learn You a erlang for Great Good!

1) Introduction

I'm currently learning scala and playing with scala actors. Pretty much every blogs mention how erlang is great for building highly scalable systems. So, wanted to learn bit about erlang itself. Did not enter erlang actor model yet but want to start with the basic stuffs.

2) Starting out

2.1 - install erlang 
brew install erlang


2.2 - Tuples

tuple in any lang is a datastructure to store a sequence of data which could be multiple types.

27> ShirtInventory = {"shirts", 100, 88}.
{"shirts",100,88}
Pattern match on tuples,
28> {Item, Qty, UnitPrice} = ShirtInventory.
{"shirts",100,88}
29> Item.
"shirts"

2.3 - List datastructure

a sequence of data of same/different types, unlike tuples can have unknown number of elements in it. has head and tail.
7> [1, 3, 5, 7] ++ [11, 13, 17, 19].
[1,3,5,7,11,13,17,19]
1> FibList = [1, 1, 2, 3, 5].
[1,1,2,3,5]

2> AnotherFibList = [0 | FibList].
[0,1,1,2,3,5]

#list pattern match

4> [Head|Tail] = AnotherFibList.
[0,1,1,2,3,5]
522> Head.
0
5> Tail.                             
[1,1,2,3,5]

6> [H | T] = Tail.
[1,1,2,3,5]
7> H.
1
8> T.
[1,2,3,5]
List comprehension - ways to build or modify lists.
[0.85*Price || Price <- [100,200,300,400]].

## get discounted prices for items in cart

2> Cart = [{"nexus", 100},{"woofer", 200},{"mic", 300},{"Drum", 400}]. 
[{"nexus",100},{"woofer",200},{"mic",300},{"Drum",400}]

3> [0.85*Price || {Item, Price} <- Cart].
[85.0,170.0,255.0,340.0]


Tuesday, 12 March 2013

Java HotSpot VM and native memory options



Java Heap Space (JHS)
Java Heap Space(JHS) is a Memory Area (generally located at bottom of address space and move upwards) inside the Java Process which holds the JVM objects.

JHS is divided into 3 REGIONs or GENs for sake of GC
1) New/YoungGen    : where all new objects are created
2) Old/TenuredGen  : where objects are placed when the NewGen overflows

Native memory
3) MetaSpace            : holds the JVM’s class{..} and method objects
                                    : contains meta-data of the classes and the objects
                                      i.e. pointers into the rest of the heap where the objects are allocated.

So, JHS = NewGen + OldGen
Metaspace is garbage collected during full GC (Full GC is cleaning the entire Heap – both YoungGen and TenuredGen.) in hotspot JVM.

GC of the dead classes and classloaders is triggered once the class metadata usage reaches the “MaxMetaspaceSize”.


                                Fig Java Heap space(JHS) regions (What is a PermGen leak?, 2012)

Relationship between Java Classes and PermGen (klass = class metadata)
Java classes are stored in the Metaspace. (Presenting the PermGen, 2006)


   Fig. Relationship between Java Classes and PermGen (Presenting the Permanent Generation, 2006)

JVM Option Categories



[1] Heap/Vrt Memory Options -> specified with -X_[n]m

-Xms         

  • Initial JavaHeapSize (default for client JVM is 1/64th part of Physical memory upto 1G)
  • Setting InitialHeapMemory equal to the MaxHeapMemory, will eliminate any need for the JVM to reallocate the Heap Memory, leaving more to be used by other memory-intensive processes. (Common Tomcat Memory Issues and How To Fix Them, mulesoft.com)

-Xmx         

  • Max JavaHeapSize (default for client JVM is 1/4th of physical memory for size upto 1G)
  • Starting the JVM with a higher Max Heap Memory will decrease the frequency with which GC occurs, eliminating excessive GC thus JVM gets optimized. (Common Tomcat Memory Issues and How To Fix Them, mulesoft.com)

If Java app is large and lots of object are created, the size of HeapSpace can be configured by using JVM options -Xms and -Xmx. (How to increase size of Java Heap, 2011)


[2] Non-heap/ Native memory options -> specified with -XX:_=[n]m

-XX:MaxMetaspaceSize  : Max  Java  Perm(anent)SpaceSize
( min/default metaspace- min is dynamically re-size depending of the application demand at runtime)

The TomCat Server runs OutOfMemory if there are too many JVM classes or huge number of Strings in the project OR due to some reference to an object or a class loaded by the application’s ClassLoader that has died after that.

Also, http://stackoverflow.com/q/2431040/432903



EXAMPLE I : -XX:MaxMetaspaceSize=512M (1/4th of 2GB)Conf (with 2G of RAM available) for tomcat server using eclipse IDE



OR Add directly the following line to {TOMCAT_DIR}/bin/catalina.sh
CATALINA_OPTS="$CATALINA_OPTS -server -Xms256m -Xmx1024m -XX:MaxMetaspaceSize=512m"


The fact is that once Java Classes are loaded, they stay in memory even if no one cares anymore.
Use the following content to {TOMCAT_DIR}/bin/catalina.sh to enable +CMSClassUnloadingEnabled so that GC will Sweep PermGen, and remove classes which are no longer used.

CATALINA_OPTS="$CATALINA_OPTS -server 
-Xms256m 
-Xmx1024m 
-XX:MaxMetaspaceSize=512m 
-XX:+CMSClassUnloadingEnabled 
-XX:+CMSPermGenSweepingEnabled"

Have a look at hotspot/src/share/vm/runtime/globals.hpp of JDK for these variables.

And other important thing here is CMS in +CMSClassUnloadingEnabled which stands for Concurrent,Mark,Sweepwhich is an algorithm/method for GC that, in theory, affects the performance of the application less than the older methods. G1st (Garbage First) is it's new competitor.


EXAMPLE II : JVM Switches for netbeans
add the following content to $NETBEANS_HOME/etc/netbeans.conf

#command line switches:
netbeans_default_options = 
"-J-Xmx1024M
–J-XX:MaxMetaspaceSize=512m 
-J-XX:+UseConcMarkSweepGC

-J-XX:+CMSClassUnloadingEnabled 
-J-XX:+CMSPermGenSweepingEnabled
-J-XX:CompileThreshold=1000"



EXAMPLE III : -XX:MaxPermSize Conf (with 2G of RAM available) For GWT Dev Mode 

<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx1024M -XX:MaxMetaspaceSize=1024m"/>

OR with +UseConcMarkSweepGC
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx1024M -XX:MaxMetaspaceSize=1024m -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled"/>

(For gwt better performance, before starting in hosted mode delete app-server/src/main/webapp/gwt-unitCache/*.*)



References

Presenting the Permanent Generation ,  2006 (HIGHLY RECOMMENDED) https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation

What is a PermGen leak?,  http://plumbr.eu/blog/what-is-a-permgen-leak

10 points about Java Heap Space or Java Heap Memory,
http://javarevisited.blogspot.sg/2011/11/hotspot-jvm-options-java-examples.html

Java HotSpot VM Options,
http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

Tuning Garbage Collection with the 1.4.2 Java[tm] Virtual Machine,
http://www.oracle.com/technetwork/java/gc1-4-2-135950.html#3.%20Sizing%20the%20Generations|outline


10 Examples of HotSpot JVM Options in Java, http://javarevisited.blogspot.sg/2011/11/hotspot-jvm-options-java-examples.html

Pick up performance with generational garbage collection, http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html

Full GC becoming very frequent, http://stackoverflow.com/a/7917221/432903

http://www.maths.lse.ac.uk/Courses/MA407/gcsurvey.pdf