Pages

Sunday, September 10, 2017

Daily News App

Read news in 9 Indian languages. Hindi ,English, Bengali , Tamil,Telugu,Kannada,Malayalam,Punjabi and Marathi. Choose your favorite news paper and read news in your own language.




 Daily News App

Sunday, September 18, 2016

Memory and Memory Management in Java/Android


There are three type of physical memory in android.

  1. RAM - Random Accessed Memory - When running an application on your phone, the application is loaded from your device memory into the RAM, just like on a computer, the main purpose for this is access speed is far greater in RAM. You can clear the apps that is in the RAM on your phone depending on your smartphone (Samsung for example hold home button down and click ram manager > clear RAM - this will clear what's in RAM however most background apps will automatically be loaded back into the RAM hence the free space decreasing straight away
  2. As you mentioned SD Card is a external memory card which you can switch and swap with a variety of different sizes if needed
  3. Device Memory is your phones built in memory, just like SD Card has memory however this one cannot be swapped or taken out of the phone. It will hold all of your phones files that make it work, just like a PC, as standard you cannot edit these files as they are read only, however through rooting your phone you may access these files and change if needed.




     To understand the memory concept and memory management you have to understand how JVM provide the memory to your code.Mainly Java code is stored in the Heap and Stack memory. The main difference between heap and stack is that stack memory is used to store local variables and function call while heap memory is used to store objects in Java. No matter, where the object is created in code e.g. as a member variable, local variable or class variable,  they are always created inside heap space in Java.

    All the local variables and method calling is in stack,  If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.

     If you are using Recursion, on which method calls itself, You can quickly fill up stack memory. Another difference between stack and heap is that size of stack memory is a lot lesser than the size of  heap memory in Java.

    Variables stored in stacks are only visible to the owner Thread while objects created in the heap are visible to all thread. In other words, stack memory is kind of private memory of Java Threads while heap memory is shared among all threads.


    What is a memory leak?

    Some objects have a limited lifetime. When their job is done, they are expected to be garbage collected. If a chain of references holds an object in memory after the end of its expected lifetime, this creates a memory leak. When these leaks accumulate, the app runs out of memory.
    For instance, after Activity.onDestroy() is called, the activity, its view hierarchy and their associated bitmaps should all be garbage collectable. If a thread running in the background holds a reference to the activity, then the corresponding memory cannot be reclaimed. This eventually leads to an OutOfMemoryError crash.

    How do I avoid memory leaks?
  4. Avoid passing Context objects further that your activity or fragment
  5. NEVER  make/store a Context or View in a static variable. This is the first sign of a memory leak.                       
  6. private static TextView textView; //DO NOT DO THIS // private static Context context;
  7. Always unregister listeners in your onPause()/ onDestroy() methods. This includes Android listeners, to things such as Location services or display manager services and your own custom listeners.
  8. Don’t store strong references to activities in your AsyncTasks or background threads. Your activity may get closed, but your AsyncTask will continue execution and hold onto that reference of your activity.
  9. Use Context-application (getApplicationContext()) instead of Context from an activity if you can.
  10. Try not to use non-static inner classes if you can avoid it. Storing reference to something like an Activity or View inside this can lead to memory leaks. Use WeakReference if you need to store reference to them.
  11. Never give image in src in xml for preview, use instead tools:src for preview. It will take image for preview only but android:src loads image in memory, so that create a memory blocker if heavy image is used.   

Saturday, January 31, 2015

2D ARRAY

Declaration :--

datatype arr[][];
      
      or

datatype [][]arr;

int a[][];


Memory Allocation:--


arr = new datatype[rowwise][colum wise];

a = new int [3][4];

marks = new int[25][5];

scales = new float[10][4];

int[][] = new int [3][4];




INITIALIZATION:--


int a[][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};


      1   2   3   4
      5   6   7   8
      9  10 11 12






















SHORTING IN JAVA

SHORTING



1.)Selection Sort:--


class sel Shot
{
 public static void main(string args[]){

int a[] = {                   };
int i,j.t;
int n= a.length;

system.out.println("Array");

for(i=0; i<n; i++)
{
system.out.println(a[i] + "\t")
}

for(i=0; i<n; i++)
{
 for(j=i+1; j<n; j++)
{
  if(a[i] > a[j])
 {
   t = a[i];
   a[i] = a[j];
   a[j] = t;
 }
}
}

System.out.println("\n shorted list");

for(i=o; i<n; i++)
system.out.print( a[i] + "\t");
   }
  }
}
 system.out.println("\n shorted array")
 for(i=o; i<n; i++)
system.out.print(a[i]+ "\t");
}
}
}



2.) Insertion Short:--


class inser Shot
{
 public static void main(string args[]){

int a[] = {                   };
int i,j.t;
int n= a.length;

system.out.println("Array");

for(i=0; i<n; i++)
{
system.out.println(a[i] + "\t")
}

for(i=0; i<n; i++)
{
 for(j=i; j<0; j--)
{
  if(a[j-1] > a[j])
 {
   t = a[j-1];
   a[j-1] = a[j];
   a[j] = t;
 }
}
}
 system.out.println("\n shorted array")
 for(i=o; i<n; i++)
system.out.print(a[i]+ "\t");
}
}
}




2.) Bubble Short:--


class bub Shot
{
 public static void main(string args[]){

int a[] = {                   };
int i,j.t;
int n= a.length;

system.out.println("Array");

for(i=0; i<n; i++)
{
system.out.println(a[i] + "\t")
}

for(i=0; i<n; i++)
{
 for(j=0; j<n-1; j++)
{
  if(a[j] > a[j+1])
 {
   t = a[j];
   a[j] = a[j+1];
   a[j+1] = t;
 }
}
}
 system.out.println("\n shorted array")
 for(i=o; i<n; i++)
system.out.print(a[i]+ "\t");
}
}
}





























SEARCHING


1.) Sequential Search(Linear Searching)


class seqSearch
{
 puclic static void main (string args[]){

int x = Integer.parseInt(args[0]);

int a[x] = {                          };

int i, pas = -1;

for(i=0; i<a.length; i++)
 {
   if(a[i] == x)
    {
      pas = i;
      break;
     }
  }
if(pas == -1)

system.out.println("value not found");
else
   system.out.println("value found at index:-" +pas);
  }
}






2.) Binary Search :--


class seqSearch
{
 puclic static void main (string args[]){

int x = Integer.parseInt(args[0]);

int a[x] = {                          };

int first = 0, last = a.length-1, mid, pas = -1;

while(first<=last)
{
 mid = (first+last)/2;
if( x > a[mid])
first = mid +1;
else if(x<a[a])
 last= mid-1;
else
{
 pass= mid;
break;
 }
}