Solved

Memory Question

Posted on 2006-11-04
4
213 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

734 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