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 8 of 8

Thread: need your help -- Instances

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default need your help -- Instances

    Hello everyone,

    I have a question regarding multiple instantiations of a class and memory management. To illustrate my problem I have provided an example with two classes: Some_Class and Some_Other_Class. The constructor of some class uses a float and an integer as arguments and has a function which computes a certain number based on Var_A, Var_B and some formula. One of the methods in the class "Some_Other_Class" is used to store the different results of the method "Some_Function" in a vector of size "some_size". If I am to write the procedure the way I have shown below, an instance of the class "Some_Class" will be created "some_size" times in the method "Some_Routine". I know Java has automatic garbage collection, which brings me to the question: will Java dispose of the class instance after the end of each iteration or will all the instances of the class "Some_Class" be stored in memory.

    ************************************************** ***********************************
    public class Some_Class {
     
    		 private float Var_A;
    		 private int Var_B;
     
                     public Some_Class(float A, int B){
    		     Var_A = A;
    		     Var_B = B;
                     }
     
    		 public float Some_Function(){
     
    		 return Var_A * Var_B * Some_Complex_Formula;
     
                     }
    }
     
     
     
    public class Some_Other_Class {
    		 .
    		 .
    		 .
                  private Vector Some_Routine(float alpha){
     
    		 Vector V = new Vector(some_size);
    		 for (int i = 0; i < some_size; i++)
                    {
    		     SomeClass Ref = new Some_Class(i * some_float/alpha, i);
    		     V.elementAt(i) = Ref.Some_Function();
                     }
    		 return V;
                  }
    }
     
    ************************************************************************************
     
    Will it be more efficient (lower impact on memory) to use an argument free constructor for the class "Some_Class" and use the arguments in the method declaration. See snippet below:  
     
    Alternative Way:
     
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    public class Some_Class {
     
     
     
                     public Some_Class(){
     
                         // empty constructor
                     }
     
    		 public float Some_Function(float Var_A, int Var_B){
     
    		 return Var_A * Var_B * Some_Complex_Formula;
     
                     }
    }
     
     
     
    public class Some_Other_Class {
    		 .
    		 .
    		 .
                  private Vector Some_Routine(float alpha){
     
    		 Vector V = new Vector(some_size);
                     SomeClass Ref = new Some_Class();
     
    		 for (int i = 0; i < some_size; i++)
                    {
    		     V.elementAt(i) = Ref.Some_Function(i * some_float/alpha, i);
                     }
     
    		 return V;
                  }
    }
    Thank you in advance
    Last edited by copeg; January 14th, 2011 at 05:53 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need your help -- Instances

    If you call the Vector's add() function, it should store it. Not sure if yours is right now.

    Edit: No, it appears that it won't. You aren't storing anything.

    Vectors are expandable.
    Last edited by javapenguin; January 14th, 2011 at 05:50 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need your help -- Instances

    Also, your Vector should have the data type it stores in <> after the word Vector.

    And it needs the wrapper class, Float, Integer, String, Character, Boolean, Long, Short, Double, etc. Not primitives.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need your help -- Instances

    Actually, Vectors and other Collections can store any type of Object, not primitives though.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: need your help -- Instances

    Javapenguin, did you read the original posters question? All those responses have absolutely nothing to do with it...

    Just my .02, but I'd say to not worry about it. Unless you are getting OutOfMemory exceptions (which you don't specify so I'd assume no), I'd worry a bit more about which route is more appropriate for the task at hand: which is more reusable, maintainable, readable, efficient, etc...probably bound to change over time if the code is evolving, but in my view is more important than worrying about a problem that isn't there.Again just my .02
    Last edited by copeg; January 14th, 2011 at 06:14 PM.

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need your help -- Instances

    Thank you for your reply guys. Actually I have no problem storing the data in the Vector. What I was wondering is if the "for" loop will cause many instances of the class "Some_Class" to be created and eat up the memory or does each instance get destroyed once its iteration ends:

    for (int i = 0; i < some_size; i++)
    {
    SomeClass Ref = new Some_Class(i * some_float/alpha, i);
    V.elementAt(i) = Ref.Some_Function();
    }

    Is it better to have the "Some_Class" constructor empty and modify the method "Some_Function" to accept arguments:

    SomeClass Ref = new Some_Class();
    for (int i = 0; i < some_size; i++)
    {
    V.elementAt(i) = Ref.Some_Function(i * some_float/alpha, i);
    }

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: need your help -- Instances

    I stand by what I said in my previous post....but anyways...the loop which creates the objects inside will obviously have a larger memory footprint (n=1 vs n=some_size). However each object looses scope for each loop, so unless references of the objects are kept they may be garbage collected at some time (from a programmers point of view they loose scope, and are therefore gone - but from the JVM's point of view when they are 'gone' is dependent upon when the garbage collector feels the need to take out the garbage). With computers these days the difference is probably negligible between the two for most simple scenarios. Me personally, all else aside with this simple example I would err on the side of n=1 (reduced memory footprint and reduce potential for garbage collection)
    Last edited by copeg; January 14th, 2011 at 09:29 PM.

  8. The Following User Says Thank You to copeg For This Useful Post:

    xenoks (January 16th, 2011)

  9. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need your help -- Instances

    Thank you for clearing this out for me Copeg. I am actually coding this for a blackberry device and the vector actually stores asymmetric as well as symmetric encryption keys. I can't use sql lite as older bbs do not support it so I am stuck using a Vector to store the objects in. This is the reason why I was worried about the memory.

Similar Threads

  1. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 07:42 AM
  2. Replies: 0
    Last Post: December 1st, 2010, 05:10 AM
  3. Multiple instances of linked list
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 07:48 PM
  4. creating a controller to allow instances to be created from keyboard
    By ss7 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 2nd, 2009, 12:30 PM