May 20, 2013

Make Java Memory Efficient

1. in getting substrings from a BIG string make a new string like here:
String mySubstring = new String(orig.split(";")[i]); 
OR new String(offset + beginIndex, endIndex - beginIndex, value);
Taken from here and here.

If you don't mind sacrificing a little accuracy vs more memory efficeiency use bloomfilter instead of hashmap.
Taken from here and here.

Use efficient designs: stuff like Flyweight Pattern! مگس وزن
use as many abstractions as you can, don't worry about it, it would pay off

I didn't know there is a method called intern for String! Note that
all string constants or literals are interned (stored in a pool and retrieved from it). This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values. Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String()

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
String s4 = new String("Rakesh");
String s5 = new String("Rakesh").intern();

if ( s1 == s2 ){
    System.out.println("s1 and s2 are same");  // 1.
if ( s1 == s3 ){
    System.out.println("s1 and s3 are same" );  // 2.
if ( s1 == s4 ){
    System.out.println("s1 and s4 are same" );  // 3.
if ( s1 == s5 ){
    System.out.println("s1 and s5 are same" );  // 4.
will return:
s1 and s2 are same
s1 and s3 are same
s1 and s5 are same

String interning speeds up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming runtimes) that rely heavily on hash tables with string keys. Without interning, checking that two different strings are equal involves examining every character of both strings. For example, XML parsers may intern names of tags and attributes to save memory. OR BE A BITCH! Use your own intern with wEAKrEFEREMCE/sOFTREFERENCE HERE, HERE. note, you use weakreferences for the keys and values so that you don't keep references for strings which you are no longer using. I'd probably go for soft references. WeakReference references an otherwise unreachable object, then it will get cleared immediately. SoftReference may be left as is. garbage collector uses algorithms to decide whether or not to reclaim a softly reachable object, but always reclaims a weakly reachable object.
private Map<String,WeakReference<String>> myInternMap = new WeakHashMap<String,,WeakReference<String>>();
public String intern(String value) {
  synchronized(myInternMap) {
    WeakReference<String> curRef = myInternMap.get(value);
    String curValue = ((curRef != null) ? curRef.get() : null);
    if(curValue != null) {
      return curValue;

    myInternMap.put(value, new WeakReference<String>(value));
    return value;

Creating a SoftReference
SoftReferences are created by passing an object to SoftReference's constructor:

Object obj = new Object();
SoftReference softRef = new SoftReference(obj);
obj = null;  

Use tricks! For example, you could represent a table of data as a series of columns with object arrays for each column, rather than one object per row. This can save a significant amount of overhead for each object if you don't need to represent an individual row. e.g. a table with 12 columns and 10,000,000 rows could use 12 objects (one per column) rather than 10 million (one per row).
Good trick for minimizing the number of objects.

Take a look here I didn't get the time to read it.

No comments:

Post a Comment