Thursday, March 22, 2012

java.lang.OutOfMemory in Java

There are 3 OutOfMemory error cases in java we can see frequently in production with default parameters.

1) Java.lang.OutOfMemoryError: Java heap space
2) Java.lang.OutOfMemoryError: PermGen space
3) java.lang.OutOfMemoryError: GC overhead limit exceeded

1) Java.lang.OutOfMemoryError: Java heap space : 

What is java heap space ?

while you try to create an object and there is not enough space in heap to allocate that object you will get this error. 

How to solve ? 

Increase the heap size memory. Use : -Xms512m  -Xmx1024m

a) In production server / linux by adding parameter in JAVA_OPTS 
b) for eclipse add in eclipse.ini file under vmargs
c) In standalone program pass in vm Arguments. 

Note : Xms represent heap size minimum. Xmx represent heap size maximum. Based on the ram you have you can allocate. 


Read more: http://javarevisited.blogspot.com/2011/09/javalangoutofmemoryerror-permgen-space.html#ixzz2KEV0BOiK

2) Java.lang.OutOfMemoryError: PermGen space

what is PermGen Space ? 

PermGen Space of heap is used to store classes and Meta data about classes in Java. When a class is loaded by a classloader it got stored in PermGen space, it gets unloaded only when the classloader which loaded this class got garbage collected. If any object retains reference of classloader than its not garbage collected and Perm Gen Space is not freed up. This causes memory leak in PermGen Space and eventually cause java.lang.OutOfMemoryError: PermGen space

How to Solve ?

Increase the permSize memory. Use : -XX:MaxPermSize=256m

By default JVM allocates 64M for perm space. You can increase it by passing argument. 

a) In production server / linux by adding parameter in JAVA_OPTS 
b) for eclipse add in eclipse.ini file under vmargs
c) In standalone program pass in vm Arguments. 

For tomcat in bin/catalina.sh add options in CATALINA_OPTS for setting the heap and perm space. You can also add below options.
-XX:+UseParallelGC -XX:+UseParallelOldGC -Xloggc:gc.log  -XX:HeapDumpPath=DumpPath -XX:+UseCompressedOops


Read more: http://javarevisited.blogspot.com/2012/01/tomcat-javalangoutofmemoryerror-permgen.html#ixzz2KEWjRaC1

3) java.lang.OutOfMemoryError: GC overhead limit exceeded

Mostly when you use HashMap objects to dump the data you may get this above error. There are 4 ways to avoid the same.

  1. Specify more memory, try something in between like -Xmx1024m first
  2. Work with smaller batches of HashMap objects to process at once if possible
  3. If you have a lot of duplicate strings, use String.intern() on them before putting them into the HashMap (String.intern will save lot of spaces on blank strings and duplicate strings).This will slow down your process. Having said memory utilization on the heap will be very less. Refer article : https://blog.codecentric.de/en/2012/03/save-memory-by-using-string-intern-in-java/ for more 
  4. Use the HashMap(int initialCapacity, float loadFactor) or HashMap(initialCapacity) constructor to tune for your case ex : new HashMap(100000).
You can Disable the error check altogether, via "-XX:-UseGCOverheadLimit". But, you will end up getting heap space OOM error. So, try to use the above methodologies to avoid or tweak your logic to avoid storing huge contents in hashMap objects.

Refer for more OOM : http://www.theserverside.com/news/thread.tss?thread_id=79323 

Eclipse Out Of Memory Error : 
If you use eclipse update memory configuration in JRE
Eclipse -> Window -> Preference -> Java-> Installed JREs -> select the running JRE -> Default VM arguments pass the arguments
"-Xms512m  -Xmx1024m -XX:MaxPermSize=256m"


4 types of Garbage Collectors : Refer : 
http://www.takipiblog.com/garbage-collectors-serial-vs-parallel-vs-cms-vs-the-g1-and-whats-new-in-java-8/

Tuesday, February 21, 2012

Browser Shortcut keys

google Chrome / Firefox shortcut key 
  ctrl + shift + delete ->  This will bring Clear browsing data

Java is Pass-by-value

In java there is no pass by reference. Its having only "pass by value".

In "primitive data type" only the value is getting passed. In object only the object reference getting passed as value. 


Refer : http://javadude.com/articles/passbyvalue.htm for detailed example compare with C++ and C.

Sunday, February 12, 2012

Important Things to know for android App development

Managing Projects

Managing project structure on android Project is very important for development. The folder structure, classes, interfaces, xml should be added in the correct folder. Android project will by default create the folder structure in eclipse under package. You need to read below and place the classes accordingly.

Android Project folder conventions  (Article : http://developer.android.com/guide/developing/projects/index.html)
Android projects are the projects that eventually get built into an .apk file that you install onto a device. They contain things such as application source code and resource files. Some are generated for you by default, while others should be created if required. The following directories and files comprise an Android project:
src/
Contains your stub Activity file, which is stored at src/your/package/namespace/ActivityName.java. All other source code files (such as .java or .aidl files) go here as well.
bin
Output directory of the build. This is where you can find the final .apk file and other compiled resources.
jni
Contains native code sources developed using the Android NDK. For more information, see the Android NDK documentation.
gen/
Contains the Java files generated by ADT, such as your R.java file and interfaces created from AIDL files.
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the theAssetManager. For example, this is a good location for textures and game data.
res/
Contains application resources, such as drawable files, layout files, and string values. See Application Resources for more information.
anim/
For XML files that are compiled into animation objects. See the Animation resource type.
color/
For XML files that describe colors. See the Color Values resource type.
drawable/
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or a Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.
layout/
XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type.
menu/
For XML files that define application menus. See the Menus resource type.
raw/
For arbitrary raw asset files. Saving asset files here instead of in the assets/ directory only differs in the way that you access them. These files are processed by aapt and must be referenced from the application using a resource identifier in the R class. For example, this is a good place for media, such as MP3 or Ogg files.
values/
For XML files that are compiled into many kinds of resource. Unlike other resources in the res/ directory, resources written to XML files in this folder are not referenced by the file name. Instead, the XML element type controls how the resources is defined within them are placed into the R class.
xml/
For miscellaneous XML files that configure application components. For example, an XML file that defines a PreferenceScreen,AppWidgetProviderInfo, or Searchability Metadata. See Application Resources for more information about configuring these application components.
libs/
Contains private libraries.
AndroidManifest.xml
The control file that describes the nature of the application and each of its components. For instance, it describes: certain qualities about the activities, services, intent receivers, and content providers; what permissions are requested; what external libraries are needed; what device features are required, what API Levels are supported or required; and others. See the AndroidManifest.xml documentation for more information
project.properties
This file contains project settings, such as the build target. This file is integral to the project, so maintain it in a source revision control system. To edit project properties in Eclipse, right-click the project folder and select Properties.
Signing your Application is important for App launch in Google market.


Saturday, February 11, 2012

Android developers should know this Technical acronyms

Android SDK - Android Software Development Kit
ADT - Android Development Tool
JDK - Java Development Kit
JDT - Java Development Tool
Emulator - Virtual mobile device to run android development apps on your computer
AVD - Android Virtual Device
APK - Android Application Package File (Its a file to install and distribute application to Android OS)
ADB - Android Debug Bridge
Ogg is a free, open container format maintained by the Xiph.Org Foundation. The creators of the Ogg format state that it is unrestricted by software patents[4] and is designed to provide for efficient streaming and manipulation of high quality digital multimedia.
dpi - Dot Per Inch
hdpi - high dot per Inch
mdpi - medium dot per Inch
ldpi - low dot per Inch
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).




If you want to be a android developer click here. Complete documentation and API's available for you.
If you questions keep post in Android Forum

Friday, February 10, 2012

How to setup / start Android development in fedora ?

How to start Android development in fedora. 


System Requirements
1) Download Fedora version > Fedora 12. If you don't have Install fedora.

2) Install JDK (Java Development Kit) in fedora.
    Execute : yum search jdk. Once you get execute "yum install java-1.6.0-openjdk" from root.

If you want to download specific java version and install : Click

3) Download Eclipse with version > 3.5 (Due to ADT [Android Development Tool] plugin dependency on Equinox need). If you don't have Download Eclipse

4) Install Eclipse Java Development Tools from Terminal with `root` permission.
yum install eclipse-jdt 


5) Download  Android SDK based on System Requirements specification (Android is available only in 32bit. So, if you have 64-bit its ok). Extract it. If (.tgz) execute command (tar -xzf android-sdk_r16-linux.tgz). It will extract into folder android-sdk-linux.
  • Copy the extracted (android-sdk-linux) folder into $HOME directory with name android-sdk-linux
  • Add into path environment variable ~/android-sdk-linux in .bash_profile file in your home directory. 
            export PATH=$PATH:$HOME/android-sdk-linux:$HOME/android-sdk-linux/tools 

6) Install Android Development Tool Plugin for Eclipse
Android offers a custom plugin for the Eclipse IDE, called Android Development Tools (ADT), that is designed to give you a powerful, integrated environment in which to build Android applications. Its easy to debug and develop in Eclipse IDE.


Start Eclipse, then select Help > Install new software...


Again click on Add button and enter a name for the another remote site (for example, "Android Plugin") in the "Name" field. In the "Location" field, enter this URL:

https://dl-ssl.google.com/android/eclipse/ 
Note: If you have trouble acquiring the plugin, you can try using "http" in the URL, instead of "https" (https is preferred for security reasons).Click OK.Add ADT plugin Eclipse.png
    • Back in the Available Software view, next to "Work with:", you should now in see in drop down list "Android Plugin", select it and in box below see "Developer Tools" added to the list. Select the checkbox next to Developer Tools, which will automatically select the nested tools Android DDMS and Android Development Tools. Click Next.
    • In the resulting Install Details dialog, the Android DDMS and Android Development Tools features are listed. Click Next to read and accept the license agreement and install any dependencies, then click Finish.
    • Restart Eclipse.



Note : If you get any socket error try again doing the same steps above again.

7) Android Emulator
The Android SDK includes a mobile device emulator — a virtual mobile device that runs on your computer. The emulator lets you develop and test Android applications without using a physical device.

Note : Android sdk is available only for 32-bit. So, if you have 32-bit or 64-bit use below.

yum install glibc.i686 glibc-devel.i686 libstdc++.i686 zlib-devel.i686 ncurses-devel.i686 libX11-devel.i686 libXrender.i686 libXrandr.i686

8) Create AVD device (Android Virtual device) in Android SDK Manager
This tool will be used for test our android application.

  • cd into the ~/android-sdk-linux directory and run tools/android to configure and create your first Android Virtual Device.
  • Select the packages, tools, api's you want to install. Click 'Install'
  • Go to Tools -> Manage  AVDs 
  • Click 'New'  and provide AVD Details
  1. where you need to specify SD card size (I will use 62MiB), name of device (I will use "android_dev1", target (Android 2.1, if you want to develop for different target, you need to go to step 2 and install SDK platform for different version).Thumb
  2. Now click on "Create AVD" which will create Android Virtual Device.

Note : There is no close button in AVD Device window. Just 'Esc'


9) Open Eclipse

Window -> Preferences -> Android
and enter a location of your Android SDK installation into SDK Location Box and click apply:
Connect android sdk with eclipse
Press OK and we are done.



Write a Simple Android Program

  • Open Eclipse and navigate to:
  • File -> New -> Project -> Android -> Android Project
  • and insert a following information to start a new Android Project:
  • PackageName (MyFirstAndroidSample), ClassName(MyFirstAndroidSampleActivity), TestClassName(MyFirstAndroidSampleTest)
  • Press Finish.
  • On your left hand side you have a "Package Explorer. Use a package Explorer to navigate to:
  • MyFirstAndroidSample (Package) -> src -> com.gubs.android
  • From there double-click on GubsFirstSampleAndroidApp.java

Replace an existing code:
package com.gubs.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class GubsFirstSampleAndroidApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        tv.setText("Hello Android..Its Gubs and my first App");
        setContentView(tv);
    }
}

Save the class or (Ctrl + S)

Testing new android application

Now that you have saved your new project you can attempt to run it by navigating to:
Run -> Run -> Android Application -> OK




You will see the above android virtual device with your program. Andriod emulator may take some time to load. Please wait.
Click "Menu / back" in the right side of the keyboard in android emulator to go to the home page of the android emulator and click applications to see your app.



If you have questions feel free to update comments and i can try if i could help.


Tutorials i tried : 
http://fedoraproject.org/wiki/HOWTO_Setup_Android_Development
http://linuxconfig.org/get-started-with-android-application-development-using-linux-and-android-sdk
http://developer.android.com/guide/index.html


Add a column in Mysql FIRST Or AFTER


MySQL tables are easy to extend with additional columns.

Add the email column to the end of the table. To insert the new column after a specific column, such as name, use this statement:
ALTER TABLE contacts ADD email VARCHAR(60) AFTER name;
If you want the new column to be first, use this statement:
ALTER TABLE contacts ADD email VARCHAR(60) FIRST;
// Below script tag for SyntaxHighLighter