3 Replies - 1157 Views - Last Post: 26 July 2011 - 12:33 PM

#1 iSimply  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-May 11

Vector garbage collection java

Posted 18 July 2011 - 03:24 AM

Hi, Below is an example of how I am using vectors. I had read somewhere after using a vector it is best practice to set it to null. but if i am am using vectors like below, in my mainMenu, I believe this is actually creating more instances of vectors, and thus if I keep going back to my main menu, I will end up using up all my resources. if using it the like in my settings menu, the vector itself is never really nulled, but its always there. I could alternatively create the vectors in the method. make them local, and just recreate everytime I revist my menu.

What would be the best practice?

package TheGame;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Ryan
 */
public class testClass {
    Vector sMenu = new Vector();
    Vector mMenu;
    private Graphics g;
    
    public static int UPKEY = 1;
    
    public void drawMenu(Vector v, int yOffset, Graphics gr){
        // draw the menu
    }
    
    public void optionsMenu(){
        int keyPressed = 0;
        sMenu.addElement("new Game");
        sMenu.addElement("Instructions");
        sMenu.addElement("High Score");
        boolean isRunning = true;
        
        while(isRunning){
            if(keyPressed == UPKEY){
                isRunning = false;
                sMenu.removeAllElements();
            }
            drawMenu(sMenu, 0, g);
        }
        
    }
    
    public void mainMenu(){
        int keyPressed = 0;
        mMenu = new Vector();
        mMenu.addElement("new Game");
        mMenu.addElement("Instructions");
        mMenu.addElement("High Score");
        boolean isRunning = true;
        
        
        while(isRunning){
            if(keyPressed == UPKEY){
                isRunning = false;
                mMenu = null;
            }
            drawMenu(mMenu, 0, g);
        }
        
    }
    
}



Is This A Good Question/Topic? 0
  • +

Replies To: Vector garbage collection java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

Re: Vector garbage collection java

Posted 25 July 2011 - 09:11 AM

A few things. First, Vectors are generic. You should be storing a Vector<String>, Vector<Integer>, Vector<Object>, etc., but never a plain Vector. You will get a deprecation warning. Second, don't getGraphics(). Override paint() instead.

What does the Vector contain? What information does it represent? If you separate the GUI from the State/Data of the program, that will save some memory. If you aren't adding a ton to the Vectors and are only storing a handful of elements, you shouldn't have to worry. Don't try and nitpick and optimize memory so much. Let the garbage collector handle it. If you need to start storing hundreds of elements, then look more at optimization and streamlining things. But for a few elements, you're not in danger of running out of memory.
Was This Post Helpful? 1
  • +
  • -

#3 iSimply  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-May 11

Re: Vector garbage collection java

Posted 26 July 2011 - 12:28 PM

My vectors are used to to store menu items. In this case foremost, but I have been using vectors in a few other parts of the project. My reason for not going for lists was I needed to import more packages.

This was where your info was so important to me, I wanted to way up the options of an app using more space, or an app using up more resources, but the picture is still a little vague to me. obviosly is a small application using very little graphics, and processor calculations, from your explination, java will do the required garbage disposal. but when an application is needing more then I need to make a more conservitive method of what i create and when i create them.

The one reason I was looking at optimising meomory was I wanted my app to work on old phones. nothing recent. not yet. just so I can try to understand the fundamentles before carrying on. Its very good taking advatage of technology, but isnt it a little better if you stick to/ or learn proper techniques before diving into anything big?

Thank you for your reply.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

Re: Vector garbage collection java

Posted 26 July 2011 - 12:33 PM

Quote

My reason for not going for lists was I needed to import more packages.

Vectors are Lists though. The Vector class implements the List interface.

For the moment, write your app as if memory isn't going to be an issue. If you have to preload a ton of objects at once, then look at optimizing. Remember that games like asteroids and tetris work on these phones. Being conservative is good, but overall it's the big choices that matter, and this doesn't seem like one of them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1