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

Thread: Manual Memory Management

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Manual Memory Management

    How can one allocate an object in a function with a new command and add it to a list, and keep it even after the function returns?
    To be more precise, here is the C++ equivalent:
    void myfunction()
    {
    MyClass* myobject = new MyClass(...);
    mylist.add(myobject);
    }
     
    void someOtherFunction()
    {
    MyClass* myobject = mylist.at(2); // The object has not been deleted
    }

    At some point, I would simply call
    delete myobject;
    to deallocate my object when I wouldn't need it anymore

    I tried doing the same with Java, but my object must be getting deleted when the function terminates because I get a null reference exception
    void myfunction()
    {
    MyClass object = new MyClass();
    mylist.add(object);
    }

    Does anyone know the way I can do this in Java? Also, how can I delete the object?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Manual Memory Management

    how can I delete the object?
    You can't. The garbage collector takes care of recovering dead objects.

    keep it even after the function returns?
    If there is a reference to the object in some variable or list outside of the method, the object will be retained.

    my object must be getting deleted when the function terminates because I get a null reference exception
    Please post the full text of the error message and a small program that compiles, executes and shows the problem. Your explanation does not make sense. Something else is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Manual Memory Management

    Thank you Norm

Similar Threads

  1. Properly releasing memory to avoid memory pileup/crash
    By fickletrick in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 22nd, 2012, 10:09 AM
  2. [SOLVED] Memory usage increasing in while loop - is it a memory leak
    By mds1256 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2012, 10:06 AM
  3. HELP ME PLEASE MEMORY MANAGEMENT PROGRAM USING JAVA
    By lockwater in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2010, 08:30 AM
  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. Java memory management
    By trueacumen in forum Java Theory & Questions
    Replies: 5
    Last Post: August 12th, 2009, 02:40 AM

Tags for this Thread