Solved

Memory Question

Posted on 2006-11-04
4
206 Views
Last Modified: 2010-03-31
Hi,

I'm writing some software and im a little worried about leaking memory.  If i create an object or array, and then assign that variable to a new instance of that object, will the memory of that previous object be freed?

Eg.

double[][] varA = new double[][];
MyObject varB = new MyObject();
....
varA = new MyObject(); // Will the object previously stored be freed?
varB = getNewArray(); // Will the array previously stored be freed?
// getNewArray() returns a pointer to a double[][] array that was created in the function

Thanks,
Michael
0
Comment
Question by:Xavior2K3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>double[][] varA = new double[][];

Only allocates a reference - nothing else

If all this is happening inside a method deallocation will occur on return
0
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
Comment Utility
if there are no references left to the object then it will be *available* for garbage collection.
When it gets freed is up to the garbage collector.

So in your situation there appears to be no other refs so it would become available for collection.
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 100 total points
Comment Utility
>>> varA = new MyObject(); // Will the object previously stored be freed?
>>> varB = getNewArray(); // Will the array previously stored be freed?

I think you meant it the other way around:

>>> varB = new MyObject(); // Will the object previously stored be freed?
>>> varA = getNewArray(); // Will the array previously stored be freed?

But the answer is: YES.
... but ... not at once, but when JAVA thinks the time is right.
So, don't worry and consider the space freed.

;JOOP!

0
 
LVL 1

Author Comment

by:Xavior2K3
Comment Utility
Great, thanks for clearing that up for me guys!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now