3

I have written a small Java program where I create 10 String Objects.

Can someone explain or answer the following questions.

  1. What are the best practices to create a Object. ie; When I create a Object should I also make sure I delete the object once its used. If, How do I delete it?
  2. If I don't delete the object, is that the object will be lying until the program ends?
  3. Is there a way to check the number of active objects when a program is running?
public class Test{

    static public void main(String[] args){
        for (Integer i = 0; i < 10; i ++){
            String s1 = new String("Creating new String");
        }
        System.out.println("Program COmpleted");
    }

}
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • 2
    you should probably read up on garbage collection http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html – Sionnach733 Mar 05 '14 at 15:30
  • 1
    To answer in part, you cannot explicitly delete objects in Java. Java uses a Garbage Collection (or GC) system, which automatically collects (and deletes) objects. Objects may very well be deleted before the program completes. – OSborn Mar 05 '14 at 15:30
  • Don't create String objects by constructor, because then you create a new object every time. It should be like that: String s1 = "Creating new String"; – Jakub H Mar 05 '14 at 15:32

5 Answers5

3

When I create a Object should I also make sure I delete the object once its used.

No. You can not explicitly delete an Object in Java. For certain memory-intensive use cases, it might make sense to explicitly nullify references to your objects, to help the garbage collector with the cleanup process, but the object itself is not deleted until it is collected by the garbage collector.

If I don't delete the object, is that the object will be lying until the program ends?

No. The garbage collector will take care of deleting it, once there are no references to it anymore. The point in time when this happens is up to the garbage collector - this might also be at the end of your application.

Is there a way to check the number of active objects when a program is running?

Yes, you can use a profiler such as YourKit.

See also

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • So, In the above case.. I have created 15 String Objects..and they have a reference until the program finishes. When will the GC run in this case? – user1050619 Mar 05 '14 at 15:35
  • It is up to the garbage collector. In your (simple) use case, it might be that the GC is not running at all since there is still enough memory available. – Andreas Fester Mar 05 '14 at 15:36
  • Ok, If there is no memory in my program, these 15 String Objects won't be GC'd, Correct?..It will be GC'd only if I set a object to NULL?, Correct? – user1050619 Mar 05 '14 at 15:37
  • Correct. As long as an Object is reachable through a reference (not counting cyclical references within a web of objects), it can not be garbage collected – Andreas Fester Mar 05 '14 at 15:40
  • So, how do I use a finalize method incase its not guaranteed that it will be run. If its not guaranteed to run, why do they exists in Java? – user1050619 Mar 05 '14 at 15:43
  • See http://stackoverflow.com/questions/8682061/when-should-be-we-use-finalize-method-in-java - thats also the only use case I came across so far, where finalizers can reliably be used: as a **last-ressort safety net**, when it is otherwise non-critical when they are **not** executed. – Andreas Fester Mar 05 '14 at 15:45
1

What are the best practices to create a Object. ie; When I create a Object should I also make sure I delete the object once its used. If, How do I delete it?

The best practices to create a Object is just when you want to use it.

Also you need to learn about variable scoping.

If I don't delete the object, is that the object will be lying until the program ends?

No, GC will kill un referenced objects

Is there a way to check the number of active objects when a program is running?

You can use any Java profiling tool such as vesualVM

Salah
  • 8,567
  • 3
  • 26
  • 43
1

Java is a Garbage Collected language, and therefore, most memory management and cleanup is done for you. This contrary to languages like C++ where one must manually free unused memory and generally be more cautious of possible memory leaks.

To address your specific questions:

  1. Sometimes, it is considered good practice to "null out" unused object references. This is usually only true in specific instances, for example see: Does setting Java objects to null do anything anymore?.

  2. No, not necessarily. If there is nothing pointing to the Object in memory, then the Garbage Collector should free up the unused memory.

  3. You could use a debugger to keep track of the number of objects that are created as you step through your code line by line (I am not sure if this is robust enough a solution for your purposes).

Community
  • 1
  • 1
Kevin Gurney
  • 1,411
  • 11
  • 20
0

Java has garbage collector. Garbage collector destroys objects when it's no longer used. you can find gc basics here](http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html)

If you want to profile application use jvisualvm , which is packaged with JDK. If you want to count objects programmatically , use JMX API's (read oracle documentation about JMX)

Sagar Gandhi
  • 925
  • 6
  • 20
0

The JVM automatically recycles objects that are no longer in use (this is called "garbage collection"). This happens when it feels like it, and when running out of previously unallocated memory, and returns the memory used by the unused objects to the unallocated memory pool.

All you have to do is to ensure that there is no variable through which you can use the objects any more - typically by assigning another value - and the rest happens automatically.

The simplest way to see how many objects are used by your program, is to use a profiler. You can use visualvm in the JDK or the one in Netbeans for free.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347