Return to site

โ˜• Java Green IT rules ๐ŸŒ๐Ÿ’š

October 6, 2024

๐ŸŒธ 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.