• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Memory Question

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
Xavior2K3
Asked:
Xavior2K3
2 Solutions
 
CEHJCommented:
>>double[][] varA = new double[][];

Only allocates a reference - nothing else

If all this is happening inside a method deallocation will occur on return
0
 
objectsCommented:
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
 
sciuriwareCommented:
>>> 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
 
Xavior2K3Author Commented:
Great, thanks for clearing that up for me guys!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now