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

Thread: Memory issues..?

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Memory issues..?

    The object of this program is to make it where you can have a list of students in a class along with 5 of their test grades and you can add, replace, or delete a student as well as change a test grade or their name. It also must print these in a neat format. Mine seems to work when first run, although the format is sloppy. However, if you wait about a minute, this error occurs

    "java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:213)
    at java.util.ArrayList.ensureCapacityInternal(ArrayLi st.java:187)
    at java.util.ArrayList.add(ArrayList.java:428)
    at TestStudent.insertStudent(TestStudent.java:80)
    at TestStudent.main(TestStudent.java:133)"

    If you look at the code you will see the program wasnt done when this error came after all. I just need to know why its doing this and how to fix it. Also if someone could show me how to make it print neatly while still using the toString() method I have made, I would greatly aprreciate it. Thanks. Heres my code:

    public class Student
    {
        private String name;
        private int qz1, qz2, qz3, qz4, qz5;
     
        public Student(String name, int qz1, int qz2 ,int qz3 ,int qz4, int qz5)
        {
            this.name = name;
            this.qz1 = qz1;
            this.qz2 = qz2;
            this.qz3 = qz3;
            this.qz4 = qz4;
            this.qz5 = qz5;
        }
     
        public void setName(String s)
        {
            name = s;
        }
     
        public String getName()
        {
            return name;
        }
     
        public void setQuiz(int quiz, int score)
        {
            if (quiz == 1)
            qz1 = score;
            else if (quiz == 2)
            qz2 = score;
            else if (quiz == 3)
            qz3 = score;
            else if (quiz == 4)
            qz4 = score;
            else if (quiz == 5)
            qz5 = score;
     
            else 
            System.out.println("Invalid quiz number. Try again.");
     
        }
     
        public int getQuiz(int i)
        {
            if (i == 1)
            return qz1;
            else if (i == 2)
            return qz2;
             else if (i == 3)
            return qz3;
            else if (i == 4)
            return qz4;
             else
            return qz5;
        }
     
        public String toString()
        {
            return (name + ":         " + qz1 + "       " + qz2 + "       " + qz3 + "       " + qz4 + "       " + qz5 + " ");
        }
     
     
    }

    That was the main class.

    This is the tester.

    import java.util.*;
    public class TestStudent
    {
     
        public static String firstName(String s)
        {
           return s.substring(0, s.indexOf(" ")); 
        }
     
        public static void printBook(List<Student> a)
        {
            System.out.println("    Name             01       02       03       04       05");
            System.out.println("---------------------------------------------------------------");
            for (Student i : a)
            {
                System.out.println(i);
            }
        }
     
        public static void replaceName(List<Student> a, String s, String t)
        {
            for(Student v : a)
            {
                if(v.getName().equals(s))
                {
                 v.setName(t);
                }
            }
             System.out.println("Changing " + firstName(s) +"'s" + " name to " + t + ":");
     
        }
     
     
        public static void replaceQuiz(List<Student> a, String s, int q, int r)
        {
            for(Student v : a)
            {
                if(v.getName().equals(s))
                {
                   v.setQuiz(q, r);
           }
        }
            System.out.println("Replacing " + firstName(s) +"'s" + " quiz " + q + " score to " + r + ":");
     
     
     }
     
        public static void replaceStudent(List<Student> a, String s, String n, int q, int w, int e, int r, int t)
        {
            for(Student v : a)
            {
                if(v.getName().equals(s))
                {
                    v.setName(n);
                    v.setQuiz(1, q);
                    v.setQuiz(2, w);
                    v.setQuiz(3, e);
                    v.setQuiz(4, r);
                    v.setQuiz(5, t);
     
                }
            }
     
            System.out.println("Replacing " + firstName(s) + " with " + n + ": " + q + ", " + w + ", " + e + ", " + r + ", " + t + ":");
        }
     
         public static void insertStudent(List<Student> a, String s, String n, int q, int w, int e, int r, int t)
        {
            int c = 0;
            for(Student k : a)
            {
     
                if(k.getName().equals(s))
                {
                a.add(c, new Student(n, q, w, e, r, t));
                }
                c++;
        }    
            System.out.println("Inserting " + n + ": " + q + ", " + w + ", " + e + ", " + r + ", " + t + ", " + "before " + firstName(s) + ":");
       }
     
        public static void deleteStudent(List<Student> a, String s)
        {
            int c = 0;
            for(Student q : a)
            {
                if (q.getName().equals(s))
                {
                    a.remove(c);
                }
                c++;
            }
     
            System.out.println("Removing " + s + ":");
        }
     
       public static void main(String [] args)
       {
            List<Student> myClass = new ArrayList<Student>();
     
           myClass.add(new Student("Mark Kennedy", 70, 80, 90, 100, 90));
           myClass.add(new Student("Maz Gerard", 80, 85, 90, 85, 80));
           myClass.add(new Student("Jean Smith", 50, 79, 89, 99, 100));
           myClass.add(new Student("Betty Farm", 85, 80, 95, 88, 89));
           myClass.add(new Student("Dilbert Gamma", 70, 70, 90, 70, 80));
     
     
           System.out.println("Starting Gradebook: ");
            System.out.println();
            printBook(myClass);
     
               System.out.println();
               replaceName(myClass,"Betty Farm", "Betty Boop");
               System.out.println();
               printBook(myClass);
     
               System.out.println();
               replaceQuiz(myClass, "Jean Smith", 1, 60);
               System.out.println();
               printBook(myClass);
     
             System.out.println();
           replaceStudent(myClass, "Dilbert Gamma", "Mike Kappa", 00, 00, 00, 90, 90);
           System.out.println();
           printBook(myClass);
     
           System.out.println();
           insertStudent(myClass, "Betty Boop", "Lily Mu", 85, 95, 70, 0, 100);
            System.out.println();
            printBook(myClass);
     
             System.out.println();
             deleteStudent(myClass, "Max Gerard");
              System.out.println();
              printBook(myClass);
     
     
     
        }
    }

    it may seem a bit hectic, if you need an explanation let me know. Thanks for the help!!
    Last edited by Saintroi; April 23rd, 2012 at 08:10 AM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Memory issues..?

    "java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:213)
    at java.util.ArrayList.ensureCapacityInternal(ArrayLi st.java:187)
    at java.util.ArrayList.add(ArrayList.java:428)
    at TestStudent.insertStudent(TestStudent.java:80)
    at TestStudent.main(TestStudent.java:133)"
    The error is saying that you have run out of memory. It also suggests that an array list was "growing" ie you were adding something to the list at the time. In general - having figured out the general sort of thing that is going wrong you read down the stack trace until you hit a line of your code: that's usually a good place to begin hunting for the bug.

    In this case it's "TestStudent.insertStudent(TestStudent.java:80 )". Unfortunately the code you posted for that method is mangled (it won't compile as you posted it.)

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Saintroi (April 23rd, 2012)

  4. #3
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Memory issues..?

    Yes, the problem was a for loop that wasnt stopping in the correct spot, so I changed it to a for each loop like the rest of the methods have. But now it throws another error:
    "java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Arr ayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at TestStudent.insertStudent(TestStudent.java:77)
    at TestStudent.main(TestStudent.java:135)"

    Also, the code would not compile because I somehow got a random 5 in there. But now I have edited it in my original post so that it will compile, and also has the change that I made to the method in question - insertStudent(); and should throw that error.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Memory issues..?

    for(Student k : a)
    {
     
        if(k.getName().equals(s))
        {
            a.add(c, new Student(n, q, w, e, r, t));
        }
        c++;
    }

    The ConcurrentModificationException occurs because as you iterate over a you add something to that array list. That is you add something to the list you are supposed to be iterating over.

    Try the other form of iteration instead:

    int sz = a.size();
    for(int ndx = 0; ndx < sz; ndx++)
    {
        Student s = a.get(ndx);
        // etc
    }

    You might find that the index ndx can take the place of c.

  6. #5
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Memory issues..?

    Alright that works, however it inserts the same person twice?

    this code

     
     public static void insertStudent(List<Student> a, String s, String n, int q, int w, int e, int r, int t)
        {
            int sz = a.size();
            for(int c = 0; c < sz; c++)
            {
                Student g = a.get(c);
     
                if (g.getName().equals(s))
                {
                    a.add(c, new Student(n, q, w, e, r, t));
                }
     
        }    
            System.out.println("Inserting " + n + ": " + q + ", " + w + ", " + e + ", " + r + ", " + t + ", " + "before " + firstName(s) + ":");
       }"

    Also, if you cant do that, why does this method work?

    public static void deleteStudent(List<Student> a, String s)
        {
            int c = 0;
            for(Student q : a)
            {
                if (q.getName().equals(s))
                {
                    a.remove(c);
                }
                c++;
            }
     
            System.out.println("Removing " + s + ":");
        }

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Memory issues..?

    however it inserts the same person twice?
    It can only insert two people if g.getName().equals(s) is true twice. Try printing the index c and the corresponding student g each time around the loop to see why this is.

    why does this method work?
    "work", I don't know. I don't really have the time to see what it *does* do and under what cirumstances (not saying that's an uninteresting question) - so I'll just accept how the API documents the behaviour: "The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

    "Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs".

    I am assuming that the enhanced for loop uses the iterator() method. Frankly I can't see any "unsynchronized concurrent modification" going on, but I'll just accept that the class's iterators *are* mostly "fail fast" but that you *cannot* rely on this.

  8. #7
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Memory issues..?

    Haha well that makes sense.. kind of.. It works for now! But after doing what you suggested, i am utterly confused. After the program runs each method (insertStudent, deleteStudent ect.) it prints the whole list of Students. And in the one right before the insertStudent method is called is printed as:

    "

    Name 01 02 03 04 05
    ---------------------------------------------------------------
    Mark Kennedy: 70 80 90 100 90
    Maz Gerard: 80 85 90 85 80
    Jean Smith: 60 79 89 99 100
    Betty Boop: 85 80 95 88 89
    Mike Kappa: 0 0 0 90 90
    "

    Now, when the insertStudent method is told to print c and g each time in the loop, heres what shows up:


    "0
    Mark Kennedy: 70 80 90 100 90
    1
    Maz Gerard: 80 85 90 85 80
    2
    Jean Smith: 60 79 89 99 100
    3
    Betty Boop: 85 80 95 88 89
    4
    Betty Boop: 85 80 95 88 89
    "

    As you can see, number 4 should have been "Mike Kappa", but for some reason the method reads 2 Betty Boop's. Then the really confusing part is when it prints the List of Students:

    "Name 01 02 03 04 05
    ---------------------------------------------------------------
    Mark Kennedy: 70 80 90 100 90
    Maz Gerard: 80 85 90 85 80
    Jean Smith: 60 79 89 99 100
    Lily Mu: 85 95 70 0 100
    Lily Mu: 85 95 70 0 100
    Betty Boop: 85 80 95 88 89
    Mike Kappa: 0 0 0 90 90
    "

    There are 2 Lily Mu's. Why? I have no idea.

  9. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Memory issues..?

    "0
    Mark Kennedy: 70 80 90 100 90
    1
    Maz Gerard: 80 85 90 85 80
    2
    Jean Smith: 60 79 89 99 100
    3
    Betty Boop: 85 80 95 88 89
    4
    Betty Boop: 85 80 95 88 89
    "
    As predicted the loop is seeing Betty Boop twice. And, hence I guess, adding Lily Mu twice which explains the other output.

    If you can't see why, you need to step through the code one line at a line: round and round the loop keeping track of the loop counter, c. A debugger is good at "stepping" through code like this. Or you can use plenty of System.out.println(). Either way you need to be aware of what the value of each variable is at each step.

    public static void insertStudent(List<Student> a, String s, String n, int q, int w, int e, int r, int t)
    {
        int sz = a.size();
        for(int c = 0; c < sz; c++)
        {
            Student g = a.get(c);
            System.out.println("At top of loop");
            System.out.println("    c=" + c + " name=" + g.getName());
     
            if (g.getName().equals(s))
            {
                System.out.println("Found match");
                System.out.println("    Before student added: a.get(c).getName()=" + a.get(c).getName());
                a.add(c, new Student(n, q, w, e, r, t));
                System.out.println("    After student added: a.get(c).getName()=" + a.get(c).getName());
            }
     
        }    
        System.out.println("Inserting " + n + ": " + q + ", " + w + ", " + e + ", " + r + ", " + t + ", " + "before " + firstName(s) + ":");
    }

  10. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Saintroi (April 24th, 2012)

  11. #9
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Memory issues..?

    Ohhhh I see. It adds the new Student in, but pushes Betty Boop up so that it sees it twice. So I should do a c++ after it inserts the new Student.

  12. #10
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Memory issues..?

    Well done. I'm glad you've got it figured out.

  13. #11
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Memory issues..?

    Thanks a lot for your help!

Similar Threads

  1. In memory concept
    By VaniRathna in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 6th, 2012, 06:48 AM
  2. In Memory CD Database
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 7th, 2011, 11:13 AM
  3. SwingWorker - max memory?
    By fractalorbit in forum AWT / Java Swing
    Replies: 1
    Last Post: September 15th, 2011, 02:58 PM
  4. Memory Handling -de allocate memory in Java
    By 19world in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2010, 04:05 AM
  5. Out of Memory Error
    By wasaki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 31st, 2010, 03:37 PM