Return to site

☕ Java Green IT rules 🌍💚

· java,greenIT

🌸 Avoid Spring repository call in loop

Using a Spring Repository interface in a loop induces unnecessary CPU calculations.


🌀 Avoid SQL request in loop

Executing SQL queries in a loop induces unnecessary CPU calculations, RAM usage, and network transfer.


🔄 Avoid the use of Foreach with Arrays

Using List instead of Arrays with Foreach saves CPU cycles and RAM consumption.


🚫 Do not call a function when declaring a for-type loop

Avoid calling functions when declaring a for-type loop to prevent function calls at each iteration. This saves CPU cycles.


🛑 Do not unnecessarily assign values to variables

Avoid unnecessarily assigning values to variables. It increases RAM usage.


❌ Don’t use the query SELECT * FROM

Database servers must resolve fields concerning the schema. Knowing and using the schema directly saves CPU cycles and network transfer.


➕ Use ++i instead of i++

The form i++ creates a temporary variable while ++i does not. This saves CPU cycles.


📏 Avoid getting the size of the collection in the loop

When iterating over a collection, get the size of the collection in advance to avoid retrieving it at each iteration. This saves CPU cycles and thus consumes less energy.


🔀 Avoid multiple if-else statement

When using too many if-else conditional statements, it impacts performance as the JVM has to compare conditions. Consider using a switch statement instead of multiple if-else statements if possible. The switch statement has a performance advantage over if-else.


💾 Free resources

The try-with-resources statement should be implemented for any object that implements the AutoCloseable interface, as it saves computing resources.


🌐 Avoid using global variables

 When calling a global variable, the interpreter engine must check that it exists in all scopes, has a value, etc. Passing global variables as arguments gives them the status of local variables within the function, saving CPU cycles.


📚 Avoid usage of static collections

 If you want to use static collections, make them final and create, for example, a singleton if necessary containing the collections. Static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks.


🧩 Avoid using Pattern.compile() in a non-static context

 This operation requires a significant amount of computing power. Using a single match saves CPU cycles and RAM consumption.


🔗 Don’t concatenate Strings in loop, use StringBuilder instead

 Strings are immutable, so each time you concatenate a string, a new string is created. This wastes memory and CPU.


🛠️ Don’t set const parameter in batch update => Put it in query 


📏 Initialize builder/buffer with the appropriate size

 If you know in advance how many characters will be added, initialize the builder/buffer with the appropriate size. They will never need to be resized, saving CPU cycles and consuming less energy.


📂 Optimize read file exceptions

 Optimizing file read exceptions involves efficiently handling potential errors when reading files to improve performance and ensure smooth program execution.


📝 Use PreparedStatement instead of Statement

 Use PreparedStatement instead of Statement because SQL will execute the query only once, whereas if you use a single statement, it will execute the query each time, causing unnecessary CPU calculations and energy consumption.


📋 Use System.arraycopy to copy arrays

 Programs spend most of their time in loops. These can be resource-intensive, especially when they involve heavy processing (I/O access). Additionally, the size of the data and processing within the loops will not allow full use of hardware mechanisms such as cache or compiler optimization mechanisms.