?
Solved

Memory Question

Posted on 2006-11-04
4
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17872858
>>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 600 total points
ID: 17873063
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 400 total points
ID: 17873064
>>> 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
ID: 17924768
Great, thanks for clearing that up for me guys!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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