Jan
06
2009
2

Java Tips: Memory Optimization for String

String is a unique object in Java. The Java Specification explains several unique properties of String in Java. We might already know some of them. First, String is unique because it can be created without new keyword.

String s = "new String";

I have to mention that you can still create String object using new keyword, like this:

String s = new String("new String");

Does both statement “exactly equals”? Well, most of you also know that this is not true. The first example will try to reuse the same object whenever possible (and is correct because String is immutable) while the second will force the creation of new String object. Consider this example:

System.out.println("b" == "b");
System.out.println(new String("b") == new String("b"));

The result of first example is “true” while the second one will give “false”.

I almost certain that experienced programmer will never create String using new in normal use. But sometime, we are forced to use that. One case that I can think of is when you parse an XML file using SAX parser.

public class Reader extends DefaultHandler {
 
    private List<String> listString = new ArrayList<String>();
 
    public void characters(char[] ch, int start, int length) throws SAXException {
 
        String content = new String(ch, start, length);
        listString.add(content);
 
    }
}

This example works correctly but is not efficient. Once you have a document like this:

<test>
    <string>String</string>
    <string>String</string>
    <string>String</string>
    <string>String</string>
    <string>String</string>
    <string>String</string>
    <string>String</string>
    <string>String</string>
    <string>String</string>
    <string>String</string>
</test>

Try to profile your application, force garbage collection and you will still have ten String objects left in the memory.

Fortunately, Java has provided a method to avoid such case. You can use String.intern() to force the application to use the same String object whenever possible. For above example, you can change the code to something like this:

public class Reader extends DefaultHandler {
 
    private List<String> listString = new ArrayList<String>();
 
    public void characters(char[] ch, int start, int length) throws SAXException {
 
        String content = new String(ch, start, length).intern();
        listString.add(content);
 
    }
}

Now, re-profile the application, force garbage collection, and you will only have one String left in the memory. You can save a lot of memory if you can make sure that there is only one instance of String with certain value in your JVM.

This method also has nice side effect. If you do a lot of String equality comparison in the application, a same String object run faster. To explain this, we can read the source code of String:

...
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}
...

If the object is same, then the method will be immediately after this line if (this == anObject). This is very fast and will save a lot of process time if your application do this operations a lot of time.

Written by Nanda Firdausi in: java, programming | Tags: , ,
Jan
05
2009
2

Java Tips: More About Initializing Collection

Some time ago I wrote an article about initializing collection in Java. I just found that Java actually provides several methods to help this task if you just want to create empty or singleton collection.

Look at this:

Collections.EMPTY_LIST
Collections.EMPTY_MAP
Collections.EMPTY_SET
Collections.singleton(...)
Collections.singletonList(...)
Collections.singletonMap(..., ...)
Written by Nanda Firdausi in: java, programming, tips | Tags:
Dec
25
2008
0

Collections of old games

Collections of my old games, when I still loved playing games :P. Today’s games are too boring for me, I don’t know why. Most of them are from my uncle, from my father’s friends, and from Jaya Plaza. This list is created without any order and I think there are still a lot that I forget, so if you have any suggestion, just tell me. Here we go:

Written by Nanda Firdausi in: funny | Tags: ,
Oct
31
2008
0

The Hidden Connection Between Windows and Google Chrome Logo

Hmmm...

Google: (saw at Windows) Hmmmm….
Google: I want to reform this…
Google: Tara….. (Google Chrome)

From: Federico Fieni via Mohamed Amine Chatti

Written by Nanda Firdausi in: funny, google, windows | Tags: ,
Sep
09
2008
1

What is the smallest positive integer that Google doesn’t know?

This is surely the craziest game ever! Currently the record is one seven eight, nine nine nine, two six one.

http://forums.topcoder.com/?module=Thread&threadID=623471&start=0

Written by Nanda Firdausi in: funny | Tags:
Aug
13
2008
0
Jul
31
2008
0

Pocoyo

Hmmmppfff… after a long debugging and bug fixing, here I am 20 minutes to 2 AM. But that’s not what I want to write about.

I want to write about Pocoyo, a pretty new cartoon (well, not that new… The first release was in 2005) for pre-school kid. Pocoyo is a little boy and during the cartoon show, he is accompanied by his friends, Pato (a duck), Ellie (an elephant), Lula (a dog) and Sleepy Bird (ehhh… a bird).

This cartoon is so hilarious and funny. Its focus is mainly about Pocoyo’s curiousness to the world around him. The cartoon teach some basic values in a very simple way, even simpler than Sesame Street or other child’s TV program.

So, if you have to take care a little kid, this show may be helpful for you. This video below is just one example. For more just go to Youtube!

Written by Nanda Firdausi in: funny | Tags: ,
Jul
15
2008
0

Wordpress 2.6

Want to know the new features of Wordpress 2.6. Here is a short video for that.

Written by Nanda Firdausi in: wordpress | Tags:
Jul
12
2008
10

Will Apple redefine portable game market?

iPhone games

The introduction of native application in iPhone and iPod touch made me wonder. With such revolutionary breakthrough in game controlling, will this device finally redefines portable game market?

In pre-iPhone era, portable game market is led by two big names, Sony PSP and Nintendo DS. Both have high resolution graphic display, but that is. There is no revolutionary improvement for their control. Players still use the same buttons used in old portable game consoles. Judging from that, even that the devices also have WiFi connection, we can safely say that both consoles are created merely for gaming.

iPhone and iPod touch came through different road path. Both devices are created not for gaming. They have superb audio visual capability but as we can see before, there is no game available for the devices. People used the device as mobile phone or as music/video player.

Together with the new iPhone SDK introduced by Apple several months ago, people saw the new prospect of the device. No less than 43 games are introduced in the first day of the AppStore. Their revolutionary touch control and tilt sensors are just perfect for portable game device. Depends on the game, the whole body of the device can act as steering wheel, as tilting table, as music instrument etc.

In my opinion, this will somehow redefine the portable game market in the future. The only advantage that PSP and DS have right now is the fact that the game industry is in their hand. They have more games and the device itself is already associated directly with game.

For Apple to catch them, it needs to introduce more games (and quality ones) to the devices. Sega with its Super Monkey Ball is a great sign for the future of the device. But more games are needed before it can take significant portable game market.

So what do you think? Will Apple redefine portable game market?

Written by Nanda Firdausi in: business, technology | Tags: , , ,
Jun
24
2008
3

Time Breakdown of Modern Web Design

From: http://tapestryjava.blogspot.com/2008/06/time-breakdown-of-modern-web-design.html

Written by Nanda Firdausi in: funny | Tags: , ,

Powered by WordPress. Theme: TheBuckmaker. PHP Scripts, OpenID