Solved

Memory Question

Posted on 2006-11-04
4
215 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 150 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 100 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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