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

Thread: Getting heap memory errors in recycling objects from multiple classes program

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting heap memory errors in recycling objects from multiple classes program

    Hi!

    I am building a simulation engine that creates and uses a couple hundred thousand objects per simulation trial, many of which are only used for a small amount of time. Not unexpectedly, I am running into out of heap memory errors because either a) I am not properly dereferencing them, or b) Java's garbage collection is working slower that my object creation. This happens over the course of about 20 trials, I need to run about 100 trials, and I have set the Java heap size to about 256mb.

    Regardless of the reason for the problem, and I admit it is likely to be programming errors on my part, I would like to be able to recycle all objects, when no longer needed, in about 30 classes, and make them available for reuse at the end of each trial. All objects in the recycled list could have their references reset, and since these objects are only referred to by others of these objects, my objectives would be met.

    With code such as the code below customized for each class, I am able to build a list of all objects in each :

        public static AbstractEntity    firstRecycledEntity = null;
     
     
        public Entity newEntity(int index, String name)
        {    Entity entity = null;
            if (firstRecycledEntity == null)
            { entity = new Entity(index, name);    
            }
            else
            {    entity = firstRecycledEntity;
                firstRecycledEntity = firstRecycledEntity.nextRecycledEntity;
            }
            return entity;
        }
     
     
        public static Entity void recycle(E entity)
        {    entity.nextRecycledEntity = firstRecycledEntity;
            firstRecycledEntity = entity;
        }

    However, I was wondering if there was any way I could do this with generics so that I did not have to copy this code to each class.

    Thanks . . .

    Phil


  2. #2
    Junior Member
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recycling objects from multiple classes that all descend from the same class

    Hi,
    In java if you want to force the destruction of an object , you have to call the finalize method.
    If those objects are instances of classes that you created, then you have to implement the finalize method in each of your classes.
    Hope that is helpful for you.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recycling objects from multiple classes that all descend from the same class

    Actually, if you look carefully at the documentation, finalize is called by the garbage collection right before an object is garbage collected; I am pretty sure that the documentation says explicitly that you should not call it yourself.

  4. #4
    Junior Member
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recycling objects from multiple classes that all descend from the same class

    Hi,
    I know that the garbage collector call that method to destroy an object..and you can do it too if you don't want to wait till the garbage collector does that for you.
    I did that before because I had a problem similar to your...it works.

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recycling objects from multiple classes that all descend from the same class

    Actually, when you call the garbage collector, it is just a request to do garbage collection; it does not actually have to do it then. Likewise, if you haven't properly gotten rid of all references to an object, then it also won't garbage collect that object. This is happening to me at the rate of about 40 megabytes a second, which is the reason for the original post. I could find the specific problem here, but it is so easy to create this problem, that I am looking for a totally different, and more robust approach.

    Thanks . . .

    Phil

  6. #6
    Junior Member
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recycling objects from multiple classes that all descend from the same class

    I don't think you get my point.
    When you implement the finalize method for your classes, that means that you are releasing all resources...you are NOT calling the garbage collector...in this case YOU ARE THE GARBAGE COLLECTOR for your objects....the objects gets destroyed immediately.
    Anyway...if you think it's not working that's fine
    Good luck finding another solution.

  7. #7
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recycling objects from multiple classes that all descend from the same class

    Actually I do get your point. But, it doesn't help as finalize is only called when the object is being garbage collected. My problem, unfortunately, is with the objects that are not being garbage collected.



    Phil

Similar Threads

  1. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 07:26 PM
  2. Which interface gives more control on serialization of an object?
    By abhishekraok2003 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 16th, 2009, 10:17 AM
  3. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM

Tags for this Thread