Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Omitting local large variable to save memory

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Omitting local large variable to save memory

    Hi all,

    I have a question regarding best practice in using local variables as my method return variable.

    I have a method like this

    myReturnObject getMyObject(String input) {
          myReturnObject myObject = null;
          try {
             myObject = helperObject.someOtherMethod().getObject(input); //getObject has return type myReturnObject
          } catch (Exception e) {
             //log any problems
          }
          return myObject;
    }

    And I'm wondering if I rewrite like this if I'll see some performance optimization benefit

    myReturnObject getMyObject(String input) {
          try {
             return helperObject.someOtherMethod().getObject(input); //getObject has return type myReturnObject
          } catch (Exception e) {
             //log any problems
          }
          return null;
    }

    myObject can be quite large -- so I'm wondering if I can omit the myReturnObject local variable instance if it'll save some work from the garbage collector.

    I welcome your input!

    Thanks,

    -Wolf


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Omitting local large variable to save memory

    What happened when you tried this and did some profiling?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Omitting local large variable to save memory

    Once the local variable myObject goes out of scope it will not be reachable by any other object, and thus will be available for garbage collection. However, the question is will that local variable take up more resources? In most VMs I am aware of, it will. But probably not as much as we might think; that reference is still stored thread-local and still takes up some resources. But this will depend on the VM.

    The code changes, too, so the execution profile and compile size might change. The latter is easy to check with javap.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Omitting local large variable to save memory

    A variable does not really need much memory. Those are only a few bytes since it is merely a pointer. Its the object itself that needs memory, and since you do not create a new object you will also not need much memory.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Omitting local large variable to save memory

    Quote Originally Posted by Cornix View Post
    A variable does not really need much memory. Those are only a few bytes since it is merely a pointer. Its the object itself that needs memory, and since you do not create a new object you will also not need much memory.
    That said, when I'm in another thread and assign the return value to yet another variable then I don't need to worry about the object being held in memory twice?

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Omitting local large variable to save memory

    Nope. An object exists exactly once in memory. Variables are just references to the position in memory (or in the case of java in heap space) at which the object exists.

Similar Threads

  1. Instance Variable vs. Local Variable
    By dicdic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2013, 08:53 PM
  2. 'local variable hides a field'
    By mindfcked in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2012, 12:15 PM
  3. scope of a class created as local variable
    By Samaras in forum Java Theory & Questions
    Replies: 6
    Last Post: August 25th, 2012, 06:01 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. Fitting a large primitive into a small reference variable
    By Phobia in forum Java Theory & Questions
    Replies: 15
    Last Post: October 23rd, 2009, 03:10 PM

Tags for this Thread