Jump to content
4 minutes ago, doeskydoes said:

I encountered an infinity loop and it was a pain but it got me wondering why do they happen...

while (true)
{
	// Infinite loop
}
4 minutes ago, doeskydoes said:

...can i safeguard my computer/browser from them or will i always have to restart when i screw up my code

Use a debugger.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7728427
Share on other sites

Link to post
Share on other sites

9 minutes ago, doeskydoes said:

so while trying to fix my java I encountered an infinity loop and it was a pain but it got me wondering why do they happen and can i safeguard my computer/browser from them or will i always have to restart when i screw up my code

either do what @Nuluvius said (use a debugger) or just run your code in a vm with limited resources. (set max cpu cores and max ram)

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7728442
Share on other sites

Link to post
Share on other sites

2 hours ago, mikat said:

either do what @Nuluvius said (use a debugger) or just run your code in a vm with limited resources. (set max cpu cores and max ram)

If this is an infinite loop when running a Java application it's running inside of a JVM so you don't need to restart your computer, just kill the JVM process.  If you're running the application from an IDE with the debugger attached then you typically just stop debugging (killing the application).

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7729203
Share on other sites

Link to post
Share on other sites

10 minutes ago, clegger said:

If this is an infinite loop when running a Java application it's running inside of a JVM so you don't need to restart your computer, just kill the JVM process.  If you're running the application from an IDE with the debugger attached then you typically just stop debugging (killing the application).

he means that his computer hangs because the infinite loop is taking 100% of the cpu :)

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7729257
Share on other sites

Link to post
Share on other sites

10 minutes ago, mikat said:

he means that his computer hangs because the infinite loop is taking 100% of the cpu :)

A computer should not become completely unresponsive from just one thread having an infinite loop.  It might be slow to respond.

 

If you expect to keep encountering this then maybe start the JVM with a lower process priority.

http://stackoverflow.com/questions/9053600/start-a-process-using-runtime-exec-processbuilder-start-with-low-priority

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7729319
Share on other sites

Link to post
Share on other sites

4 hours ago, mikat said:

...just run your code in a vm with limited resources. (set max cpu cores and max ram)

1 hour ago, clegger said:

If this is an infinite loop when running a Java application it's running inside of a JVM so you don't need to restart your computer, just kill the JVM process.

No. Just no... I am absolutely astonished at these suggestions to be honest. You don't want to just try things out inside a virtual machine that way or by winging it and then killing the process when it doesn't work out. You want to set up your development environment such that you can properly debug the code that you are writing. This is called doing things properly and it is every bit a part of writing software as actually typing the instructions...

 

Feel free to go right ahead an ignore me though, it's your time and productivity that you will be wasting trying to guess what's going wrong, where and why. Just wait until you try to do something with threading...

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7729537
Share on other sites

Link to post
Share on other sites

1 hour ago, Nuluvius said:

No. Just no... I am absolutely astonished at these suggestions to be honest. You don't want to just try things out inside a virtual machine that way or by winging it and then killing the process when it doesn't work out. You want to set up your development environment such that you can properly debug the code that you are writing. This is called doing things properly and it is every bit a part of writing software as actually typing the instructions...

That's all well and good if you're stepping through your own code or other compiled code that has the debugging symbols left in.  But if you're running release-compiled bytecode you can't just debug into it.  If you get into the situation where you have to stop a running java application I was simply suggesting that there are more appropriate ways to deal with it than restarting your computer or creating a VM to act as a sandbox.

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7730017
Share on other sites

Link to post
Share on other sites

1 minute ago, clegger said:

That's all well and good if you're stepping through your own code or other compiled code that has the debugging symbols left in.  But if you're running release-compiled bytecode you can't just debug into it.  If you get into the situation where you have to stop a running java application I was simply suggesting that there are more appropriate ways to deal with it than restarting your computer or creating a VM to act as a sandbox.

None of which is even in the context of what this thread is about...

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7730025
Share on other sites

Link to post
Share on other sites

if you're worried about an infinite loop, put

		try {
			// This value is set to prevent locking the host CPU at 100%
			sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

in your loop :)

Alternatively, put some kind of counter in the loop, a bit like this:

 

int counter = 0;
while (counter < n && condition == true){
                // n is a limiter to stop the loop after n runs, regardless of the status of the condition if you don't know
  	 	// if the condition will ever become false
                  
                  do Stuff;
                  
                  counter++;
                  }

Or you can just be more careful when you write your code :)

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7730676
Share on other sites

Link to post
Share on other sites

13 minutes ago, Fetzie said:

if you're worried about an infinite loop, put


		try {
			// This value is set to prevent locking the host CPU at 100%
			sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

in your loop :)

Alternatively, put some kind of counter in the loop, a bit like this:

 


int counter = 0;
while (counter < n && condition == true){
                // n is a limiter to stop the loop after n runs, regardless of the status of the condition if you don't know
  	 	// if the condition will ever become false
                  
                  do Stuff;
                  
                  counter++;
                  }

Or you can just be more careful when you write your code :)

These suggestions... they just get worse and worse... I really hope that you don't actually get to deal with any kind of production code with that kind of mentality.

 

You must be joking right?

  1. Thread sleep is bad. It's as simple as that. It is total abuse and you should simply never use it outside of any kind of testing/messing around context. Using it as a method of control is simply ridiculous.
  2. Using some kind of variable to 'short circuit' process flow is almost as bad. It is blatant hackery; injecting complicated and meaningless crap into your code.

Absolutely unbelievable, this thread now deserves a place in the hall of shame.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7730727
Share on other sites

Link to post
Share on other sites

Just now, Nuluvius said:

These suggestions... they just get worse and worse... I really hope that you don't actually get to deal with any kind of production code with that kind of mentality.

 

You must be joking right?

  1. Thread sleep is bad. It's as simple as that. It is total abuse and you should simply never use it outside of any kind of testing/messing around context. Using one as a method of control is simply ridiculous.
  2. Using some kind of variable to 'short circuit' process flow is almost as bad. It is blatant hackery; injecting complicated and meaningless crap into your code.

Absolutely unbelievable, this thread now deserves a place in the hall of shame.

Yeah, they are hacks. No, they shouldn't be in production code. But if you want a quick and dirty way to make sure the loop doesn't do something that you don't expect, they do the job while you figure it out. When you figure out why the loop never ends, you can take it out really easily.

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7730732
Share on other sites

Link to post
Share on other sites

8 minutes ago, Fetzie said:

Yeah, they are hacks. No, they shouldn't be in production code. But if you want a quick and dirty way to make sure the loop doesn't do something that you don't expect, they do the job while you figure it out. When you figure out why the loop never ends, you can take it out really easily.

Using the debugger would be much faster and easier than any of that shit and one would not be left trying to guess what was happening...

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7730765
Share on other sites

Link to post
Share on other sites

On 5/11/2016 at 7:15 AM, Nuluvius said:

Using the debugger would be much faster and easier than any of that shit and one would not be left trying to guess what was happening...

This... Use a debugger.

 

And infinite loop is occurring because the condition to exit the loop isn't getting to the point you're expecting it to:

e.g

while (condition == true)
{
	//Do the thing here...
}

//Then do this

If that condition never equates to false, then it won't move past that loop. Using a proper debugger and setting breakpoints will allow you to view variables as your program moves through the loop, and in turn, allows you to pinpoint where it's breaking

 

Assuming you're a student and they have you using Eclipse as an IDE, have a read through this OP

 

Alternatively if you're using a Text Editor + JDK in command line, read this instead

CPU: Intel Core i7-4770k | Mobo: MSI Mpower Max | Cooling: Cryorig R1 Ultimate w/ XT140 front Fan | GPU: EVGA GTX 770 Dual SC SLI | Case: NZXT H440 | Case Fans: Phanteks PH-140SP x5 | PSU: EVGA Supernova P2 1000W | RAM: 16GB Crucial Ballistix Tactical Tracer | SSD: Kingston HyperX 3k 120GB | HDD: Seagate Barracude

Keyboard: Razer Blackwidow Ultimate 2013 | Mouse: Razer Deathadder 2013 | Headphones: Sennheiser HD438s | Mousepad: Razer Goliathus Control | Monitor 1: Benq XL2430T | Monitor 2: BenQ RL2455HM 

 

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7752799
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

I'd put something inside that will set the boolean's value to false, thus exiting the while loop. Also put a try catch block so that you can handle exceptions properly. It'll be a big help if you could post the actual code.

CPU: Intel Core i5-6600k 4.4GHz | Motherboard: Asus ROG STRIX Z270F Gaming | Cooler: Cryorig H7 | RAM: GSkill Ripjaws V 8GB 2x4 3200 MHz | GPU: MSI GTX 1070 Gaming X | PSU: Seasonic G-550w 80+ Gold Certified, Semi Modular | Storage: 250GB Samsung 850 EVO, 1TB Western Digital Caviar Blue | Case: NZXT S340 Elite (Black/Red) | Monitor: BenQ XL2411 144hz | Keyboard: Corsair STRAFE RGB Cherry MX Silent | Mouse: Corsair M65 Pro RGB

 

I'd like to make a Chemistry joke, but all the good ones ARGON. *nudgenudge *winkwink

Link to comment
https://linustechtips.com/topic/594465-infinity-loops/#findComment-7844924
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×