Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2009

    Smile Memory Usage of Variables in Java


    In short, here is my case:

    I want to make a function to read XML file. In addition, I want to find a way to save as much memory for my application.

    I don't know which ways is better for me:

    Case 1:

    Class XmlParser
    //some kinds of parser
    private Xml parser = null;

    private static XmlParser instance;

    public XmlParser()
    parser = new Xml();


    public static void getInstance()
    if(instance == null) instance = new XmlParser();
    return instance;

    In this case, I create an instance to point to this class, so it won't be create again when I call the XmlParser

    e.g XmlParser.getInstance().parse(fileUrl);

    Whenever I need to use a parser, I just call a method in it and pass the file URL. It will create a new streamInput to pass to the parser;

    Note: the "parser = new Xml()" is called only one time when the variable is created first time.

    Case 2:

    public class XmlParser
    private Xml parser = null;

    public XmlParser(String fileUrl)
    //create input stream to the file and pass to the new parser

    // some methods which can be called to start parsing

    In this case, when I want to use the Parser, I must create a new instance of this class.

    e.g XmlParser parser = new XmlParser("abc.xml");
    An_Object a = parser.parse();

    Could you please compare these two cases for me. Which one is the better way when I want to use the xml parser and want to save memory too (like optimization, maybe).

    Note: I am using pull parser. About parsing xml in my application, the parser will be called when the app is started (only 1 time) and when a user changes the configuration of the app (e.g. he changes configuration of the app 1 times = 1 call to the parser, 2 time = 2 calls to the parser, etc.).

    Thank you.
    Last edited by trandinhduy; 2009-04-05 at 02:29.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Cheshire, UK

    Re: Memory Usage of Variables in Java

    Create the parser only when you need it.

    Two reasons not to create one and re-use it:

    1. It takes up memory even when you're not using it.

    2. Second time you use it, it may still have data remaining in it from the last time. A "use-once" object requires less testing.


Similar Threads

  1. Java memory allowance.
    By omgjingles in forum Mobile Java General
    Replies: 2
    Last Post: 2009-03-22, 22:04
  2. Large(complex) gif display and memory usage
    By WhoKnows in forum Symbian
    Replies: 10
    Last Post: 2008-05-08, 13:11
  3. 6230 Memory with MMC for JAVA apps
    By Insertion in forum General Development Questions
    Replies: 2
    Last Post: 2008-04-20, 18:29
  4. 3300: Crash on installing Java applications from Memory Card
    By Jhn314 in forum Mobile Java General
    Replies: 0
    Last Post: 2003-08-19, 14:18
  5. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts