This discussion is archived
3 Replies Latest reply: Mar 9, 2013 1:31 AM by Kayaman RSS

Dynamic Structure / Element building

995689 Newbie
Currently Being Moderated
Hallo, I am new to Java and have some problems at building Elements and handling like passing them to some dynamic structure and letting the references be handled without losing the elements in memory.
There are also some questions about general memory usage. I hope it gets visual at this small testclass. Maybe you can pass me throuh to some reference post at topic?

import java.util.*;
import javax.swing.*;

class Testvec{

          
     public static void main(String args[]){
     
          Vector<String> ob = new Vector<String>();
          
//          String string[] = new String[1000000]; @tested to add this at vector, but cant remove
          
          System.out.println(ob.size());
          
          long mem = Runtime.getRuntime().totalMemory(); //- Runtime.getRuntime().freeMemory();
                                             
          //@1 What about that here? total and free almost same value
          
          long time = System.nanoTime();
          
          System.out.println("Total Runtime Mem: "+Runtime.getRuntime().totalMemory()/Math.pow(2,20));
          System.out.println("Free Runtime Mem: "+Runtime.getRuntime().freeMemory()/Math.pow(2,20));
          
          JOptionPane.showInputDialog(null,"Runtime stopped");

          //@2 time to look at taskmanager, the value differs to total runtime memory @doubled
          
          time = System.nanoTime();
          for (int i = 0; i < 1000000; i++){
               
//               string[i] = Integer.toString(i);
          
               ob.add(Integer.toString(i));
               
          }
          time = System.nanoTime()-time;
          mem = Runtime.getRuntime().totalMemory(); //- Runtime.getRuntime().freeMemory() - mem;
          
          System.out.println("Total Runtime Mem: " +Runtime.getRuntime().totalMemory()/Math.pow(2,20));
          System.out.println("Free Runtime Mem: " +Runtime.getRuntime().freeMemory()/Math.pow(2,20));
          
          System.out.println("Memory taken @ " mem/Math.pow(2,20) " MBytes" +"\n" +"Time nedded @ " time/Math.pow(10,9) " seconds");
          
          System.out.println(ob.size());
          
          
          //@QUESTION I just want to know how to remove from vector. If i put it in the building loop
          //and remove it immediately after adding, memorysize does not grow. Here like you see the last
          //element is removed, but stays in memory. How to fix it?
          //maybe I have to build the added element in another way? and not at adding but look @tested
          for (int i = 0; i < 1000000; i++){     
               
               ob.remove(ob.size()-1);
//               string[i] = null;
          }
          
          System.out.println(ob.size());
          
          //Here I ve tried clearing at once, but does not work either that way. The vector is empty and I ve lost Strings in memory

     //     ob.removeAllElements();
     //     ob.clear();
     //     ob = null;
     // System.gc();
          
          JOptionPane.showMessageDialog(null,"HalloWindow");//@3 time to look at taskmanager, the value differs to total runtime
          //but not that much anymore and there is a little bytechange(seems to have some cases?) if you run sometimes again
     }
     
}
  • 1. Re: Dynamic Structure / Element building
    Kayaman Guru
    Currently Being Moderated
    When posting code, but it between two
     tags. That way it'll format nicely.
    
    But your post seems to be about memory management. Java handles that automatically, and it does that very well. You're basically wasting time looking at the task manager and checking what System.freeMemory() returns.
    You need to worry about memory management when you run out of memory. If you're new to Java, you should learn the basic things in the tutorials and leave the advanced topics for later.
    
    As for your Vector problems (ArrayList is more popular these days, Vector is kinda outdated). You can't add a String array to a Vector<String>. You also didn't fill your String array with any Strings, so you have an empty array.
    
    I recommend you to go through the tutorials, and if you have a problem with them (or are uncertain about something) then ask: http://docs.oracle.com/javase/tutorial/                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 2. Re: Dynamic Structure / Element building
    995689 Newbie
    Currently Being Moderated
    Hmm, sorry... the format and some bytes were lost between copy and paste.

    What do I do while adding "Integer.toString(number_of_loop)" and adding it at vector or arraylist?
    And what does happen when I remove them while nobody holding references?

    While trieing to add the Array of Strings, I just take one single member putting it in, as a single string trieing to handle them both then... tried also to build dynamic array myself but having problems with memory managment, cant get back to the amount of startup, even backwards does not work... Maybe I should trie interface building object? Have fun there.
  • 3. Re: Dynamic Structure / Element building
    Kayaman Guru
    Currently Being Moderated
    992686 wrote:
    Hmm, sorry... the format and some bytes were lost between copy and paste.
    Edit your post and use the
     tags to format the code.
    
    
    And what does happen when I remove them while nobody holding references?
    If nobody has references to the objects, the garbage collector will collect them. Just putting them in an ArrayList won't lose them, the ArrayList will still have references to the objects.
    tried also to build dynamic array myself but having problems with memory managment
    No, you have no problems with memory management.
    cant get back to the amount of startup, even backwards does not work...
    That's because you're trying to do irrelevant things. Basically you're wasting your time.
    Maybe I should trie interface building object?
    No, you should learn the basics of Java by going through the tutorials.
    Have fun there.
    Thanks, you too!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points