In C++, dynamically allocated array has to be deleted unless it is lost in the memory. In java, do we have to do the same, and if so. How do you do that
-
7no, it is garbage collected.Saurabh Batra– Saurabh Batra2014-01-31 10:05:06 +00:00Commented Jan 31, 2014 at 10:05
-
In Java you don't have to worry about "common" memory leaks... The in-built garbage Collector will take care of it...TheLostMind– TheLostMind2014-01-31 10:05:26 +00:00Commented Jan 31, 2014 at 10:05
-
Look this: stackoverflow.com/questions/17647194/…Nishant– Nishant2014-01-31 10:06:38 +00:00Commented Jan 31, 2014 at 10:06
-
1@TheLostMind Java has "common" memory leaks too if you keep at least a strong reference of the array somewhere (f.e. as member in a singleton class) although you don't use it. The strong reference will prevent the array from getting eligible for garbage collection. In that case you have to null-out the reference to the array.Roman Vottner– Roman Vottner2014-01-31 10:08:56 +00:00Commented Jan 31, 2014 at 10:08
-
@RomanVottner - I take that under the "not-common" category.. By common I just mean objects (arrays) etc...TheLostMind– TheLostMind2014-01-31 10:11:23 +00:00Commented Jan 31, 2014 at 10:11
3 Answers
In the Java programming language, dynamic allocation of objects is achieved using the new operator. An object once created uses some memory and the memory remains allocated till there are references for the use of the object. When there are no references for an object, it is assumed to be no longer needed and the memory occupied by the object can be reclaimed.There is no explicit need to destroy an object as java handles the de-allocation automatically. The technique that accomplishes this is known as Garbage Collection.Programs that do not de-allocate memory can eventually crash when there is no memory left in the system to allocate. These programs are said to have memory leaks.
In Java,Garbage collection happens automatically during the lifetime of a java program, eliminating the need to de-allocate memory and avoiding memory leaks. In C language, it is the programmer’s responsibility to de-allocate memory allocated dynamically using free() function. Read more at http://www.javatutorialhub.com/java-garbage-collection.html#wpewoJfMWffgXd8O.99
Comments
I don't understand your definition of dynamic array. Do you mean a collection of items where its length is unknown initialiazing it? If yes, you're talking about collections such as an ArrayList.
You don't have to worry about the deletion of your objects. The JVM will take care of your objects when they're not used anymore or when they're out of scope.
The JVM analyses the execution of your code and it will invoke a special component, named Garbage Collector, that it will clean the execution code from useless/out of scope objects.
For example, when you have this code:
if(condition)
{
string myStr = "test";
//other code here
}
The variable myStr, because it's not used anymore (it's out of scope of the if statement), will be marked by the JVM to be garbage collected.
Comments
The developer need not worry about deallocating the memory in java. The Java Garbage Collector do that for you. You can read how it works here