1

I have Simple Forever Loop like this into the System this is MVCE Example of my System.

public static void main(String args[]){
        while (true) {
            String string = new String("Hello World");
            System.out.println("String " + string);
        }

    }

according to the OOPS there will be new Object that will be created in all the iteration that will take place. and till that memory will not be free then it will run out of heap space error.

then how could i prevent the CPU for that and how could i overcome that problem.

Similarly I have for the literal like this.

public static void main(String args[]){
        while (true) {
            String string = "Hello World";
            System.out.println("String " + string);
        }

    }

for the second case if there is all time referenced only one literal which is created in string constant pool. then it should not be give out of memory error.

but i am not getting how it prevent the error that coming.

Please i like to have your Suggestion for the same i have read much of the principals of the oops and that is what i have as confusion.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
  • The problem which you want to prevent doesn’t exist. You will never get an out of memory error. Learn about what garbage collection does for you. By the way *both* loops create new strings, as the concatenation of `"String "` and `string` creates a new temporary instance which is passed to `System.out.println`. – Holger Nov 05 '14 at 12:06
  • i have already learn because the first thread taking 100 cpu so it wont let garbage collector to be execute. if m not wrong. so there has to be some way to execute it because its low priority thread so it need to be execute forcefully. – Kishan Bheemajiyani Nov 05 '14 at 12:08
  • You didn’t learn, you are guessing. It is completely irrelevant whether your thread consumes 100% cpu time. It is guaranteed that garbage collection takes place *before* a new allocation fails. The way it will happen, is unspecified. It may imply that your thread is stopped for gc, but it may also be that your thread is enforced to do the gc, so you won’t notice the difference (as the garbage collection will also consume 100% cpu time). The only thing you will notice for granted is that you will never experience an `OutOfMemoryError` with that loop. Just try it… – Holger Nov 05 '14 at 12:12
  • 1
    @Krishna The GC will run. The CPU may be going to 100% but the JVM will have inserted 'safepoints' at the method calls, and probably the back edge of the while loop too. A safepoint is a place where the JVM checks to see if it needs to stop the application threads in order to perform GC (amongst other things too). – Chris K Nov 05 '14 at 12:15
  • So if there is possibility of running out of memory JVM calls Garbage Collector is it? but what if that object keeps on allocating. ? – Kishan Bheemajiyani Nov 05 '14 at 12:17
  • @Krishna you can prove that the GC runs at all by passing `-verbose:gc` to the java command; it will echo to stdout every time it runs. – Chris K Nov 05 '14 at 12:17
  • @ChrisK how to pass that command ?? – Kishan Bheemajiyani Nov 05 '14 at 12:17
  • @Krishna you are not holding on to the references and thus the GC will always clear out the objects. Try adding the strings to an instance of ArrayList, you will soon run out of memory then. – Chris K Nov 05 '14 at 12:18
  • @ChrisK i got your point Chris and ya even if i use static instance of list then it will soon be out of memory isnt it? – Kishan Bheemajiyani Nov 05 '14 at 12:23
  • 1
    @Krishna how one passes the command will depend on how one is running java; from the command line it would be `java -verbose:gc a.b.c.Main`; I assume that you are using eclipse which I do not know, but it will have an option in its run dialog to add that flag. – Chris K Nov 05 '14 at 12:23
  • 1
    @Krishna that is correct, a static instance would have the same effect. – Chris K Nov 05 '14 at 12:24
  • @ChrisK yes your are right it will show when GC will run thanks man.. – Kishan Bheemajiyani Nov 05 '14 at 12:24

1 Answers1

5

The CPU is maxing out because the code is running in a tight loop, stalling only slightly on IO as it writes to stdout. To use less CPU, throttle the loop slightly by add a Thread.sleep(10) in the while loop. As for the string not getting GC'd, yes it will as the reference to the object will be dropped as the while loop restarts.

A way to convince yourself of this is to run jvisualvm with the Visual GC plugin and you will be able to watch the heap sizes as your program runs. The eden space will grow for a bit, and will then drop back down to zero. The other generations will remain empty.

JVisualVM is part of the JDK, so you will probably have that installed already. For the Visual GC plugin click on tools->plugins->available plugins) and you should find it in the list, depending on your firewall that is.

Once up and running JVisualVM, with the Visual GC plugin will look like this:

enter image description here


To see your program run out of memory, give the following code a go. It will add every instance of string into a list, which has a strong reference held to it from outside of the while loop.

public static void main(String args[]){
    List list = new java.util.ArrayList();
    while (true) {
        String string = new String("Hello World");
        System.out.println("String " + string);

        list.add( string );
    }

}
Chris K
  • 11,622
  • 1
  • 36
  • 49
  • jvisualvm is that plugin is available with eclipse? – Kishan Bheemajiyani Nov 05 '14 at 11:54
  • @Krishna jvisualvm is a tool that ships with the jdk, just type jvisualvm at the command line to start it. I have added a link, and some notes on how to install the plugin above. – Chris K Nov 05 '14 at 11:57
  • yes dude i have started now how could i see the Heap working? – Kishan Bheemajiyani Nov 05 '14 at 11:59
  • @Krishna have you installed the 'visual gc' plugin? if not do that first. Then start your program. When jvisual vm is running it will detect all jvms running on your local box, click on it and a new tab will open on the right hand side. From there click on the 'Visual GC' tab. I will upload a screen shot shortly to help you find it. – Chris K Nov 05 '14 at 12:01
  • I am getting VISual vm and there is number of graphs that is showing GC and CUP activity is that it? – Kishan Bheemajiyani Nov 05 '14 at 12:05
  • @Krishna no, that sounds like the first screen that appears upon attaching to the JVM process. Click on the tab labelled 'Visual GC', See the screen shot above. If that tab is missing for you, then you have not installed the plugin. Inwhich case, follow the instructions in the question above; namely click on 'Tools->Plugins->Available Plugins' – Chris K Nov 05 '14 at 12:07