I wrote a small programm for a friend with only one Thread used.

That programm does intensive work with 2d arrayLists and reaches 25% cpu utilization in my i3 cpu (4 cores).

He complained to me, that his 2 core cpu use, reaches 96% but I think that shouldn't happen. I think it should reach about 50%.

Are there any other factors in a java programm that I should take care of?

# Cpu utilization reaches 100% in two core cpu.

Page 1 of 1## 7 Replies - 316 Views - Last Post: 15 June 2019 - 12:30 PM

##
**Replies To:** Cpu utilization reaches 100% in two core cpu.

### #2

## Re: Cpu utilization reaches 100% in two core cpu.

Posted 02 June 2019 - 12:33 PM

Surely the person best placed to answer the question about why an application is overtaxing the cpu is the person who wrote that application?

### #3

## Re: Cpu utilization reaches 100% in two core cpu.

Posted 03 June 2019 - 10:39 PM

g00se, on 02 June 2019 - 12:33 PM, said:

Surely the person best placed to answer the question about why an application is overtaxing the cpu is the person who wrote that application? />

I agree with you a 100%. But I always test an application, on my machines too.

I tested it also in my 2 core cpu, that is installed on my desktop and it reaches exactly 50% usage, as it was expected. So, how can the same cause, have different effects?

I begin to wonder if users really know what kind of hardware is installed in their computers...

### #4

## Re: Cpu utilization reaches 100% in two core cpu.

Posted 04 June 2019 - 02:58 AM

>>So, how can the same cause, have different effects?

How do you know the app is doing exactly the same thing on both machines?

How do you know the app is doing exactly the same thing on both machines?

### #5

## Re: Cpu utilization reaches 100% in two core cpu.

Posted 04 June 2019 - 02:28 PM

g00se, on 04 June 2019 - 02:58 AM, said:

>>So, how can the same cause, have different effects?

How do you know the app is doing exactly the same thing on both machines?

How do you know the app is doing exactly the same thing on both machines?

The only thing this app does is reading a .csv file, saving the data in an arraylist and then running a greedy algorithm for a little time, about 2-3 minutes.

Since I know that his machine has enough memory, I don't see a reason to have such a high cpu usage.

The only thing that is left, is that he hasn't a two core cpu.

### #6

## Re: Cpu utilization reaches 100% in two core cpu.

Posted 05 June 2019 - 02:30 AM

Sounds reasonably simple, so if you were to post it here, someone might take a look and check it for possible optimization

### #7

## Re: Cpu utilization reaches 100% in two core cpu.

Posted 07 June 2019 - 03:22 AM

### #8

## Re: Cpu utilization reaches 100% in two core cpu.

Posted 15 June 2019 - 12:30 PM

Hallo again,

I will describe a little the problem I solved and then write just the method that solves the problem.

The code, runs a greedy algorithm for minimum set cover.

First, the programm reads a .csv file, with integers. This file will be saved in an ArrayList of ArrayLists of Integers.

Suppose we have a .csv file like that:

1,2,3,4

1,2,3,5

1,2,4,6

Then we save this into an ArrayList of ArrayLists, size 3 and each ArrayList has length 4. Let's call this arraylist Columns.

The question is:

How many arraylists of length n,wher n are the common integers and n<=4 do we need in order to cover the whole arraylists? I give an example:

If the user wants length 3, then 2,3,4 covers the first arraylist, 2,3,5 covers the second and 2,4,6 covers the third one. So, a solution might be

[2,3,4]

[2,3,5]

[2,4,6]

but this is not the best solution since I can cover the arraylists with only two arraylists of length 3 and these are

[1,2,3]

[1,2,4]

What if the user wants a cover of length 2? The best solution comes with only one arraylist:

[1,2] because it covers all 3 arraylists.

How I came to a solution now.

Step 1:

First of all, in order to find a very good solution, we must calculate all subarraylists of these arraylists, of length 3.

For the 1st arraylist we have

[1,2,3],[1,2,4][1,3,4][2,3,4]

for the 2nd

[1,2,3],[1,2,5][1,3,5][2,3,5]

for the 3rd

[1,2,4],[1,2,6][1,4,6][2,4,6]

These are all possible subarraylists of length 3. If I watch carefully, I can see that I have some common subarraylists and these are [1,2,3], [1,2,4] etc.

So generate a an arrayList of arraylists of integers with these subarraylists and allow no duplicates. Let's call this arraylists SubColumns.

So we have the Columns, we have the SUbColumns and step by step we have to compare each of the arraylist in SubColumns against the Columns and see which subarraylist covers the most of them. And do this in a while loop, until there are no more to compare, since all of them are covered.

We are going to use a method, isCovered(ArrayList<ArrayList<Integer>> sub1, ArrayList<ArrayList<Integer>> arr1) that returns true if a subarraylist covers an arraylist.

I will describe a little the problem I solved and then write just the method that solves the problem.

The code, runs a greedy algorithm for minimum set cover.

First, the programm reads a .csv file, with integers. This file will be saved in an ArrayList of ArrayLists of Integers.

Suppose we have a .csv file like that:

1,2,3,4

1,2,3,5

1,2,4,6

Then we save this into an ArrayList of ArrayLists, size 3 and each ArrayList has length 4. Let's call this arraylist Columns.

The question is:

How many arraylists of length n,wher n are the common integers and n<=4 do we need in order to cover the whole arraylists? I give an example:

If the user wants length 3, then 2,3,4 covers the first arraylist, 2,3,5 covers the second and 2,4,6 covers the third one. So, a solution might be

[2,3,4]

[2,3,5]

[2,4,6]

but this is not the best solution since I can cover the arraylists with only two arraylists of length 3 and these are

[1,2,3]

[1,2,4]

What if the user wants a cover of length 2? The best solution comes with only one arraylist:

[1,2] because it covers all 3 arraylists.

How I came to a solution now.

Step 1:

First of all, in order to find a very good solution, we must calculate all subarraylists of these arraylists, of length 3.

For the 1st arraylist we have

[1,2,3],[1,2,4][1,3,4][2,3,4]

for the 2nd

[1,2,3],[1,2,5][1,3,5][2,3,5]

for the 3rd

[1,2,4],[1,2,6][1,4,6][2,4,6]

These are all possible subarraylists of length 3. If I watch carefully, I can see that I have some common subarraylists and these are [1,2,3], [1,2,4] etc.

So generate a an arrayList of arraylists of integers with these subarraylists and allow no duplicates. Let's call this arraylists SubColumns.

So we have the Columns, we have the SUbColumns and step by step we have to compare each of the arraylist in SubColumns against the Columns and see which subarraylist covers the most of them. And do this in a while loop, until there are no more to compare, since all of them are covered.

We are going to use a method, isCovered(ArrayList<ArrayList<Integer>> sub1, ArrayList<ArrayList<Integer>> arr1) that returns true if a subarraylist covers an arraylist.

// We generate an arraylist that holds records of left subcolumns and columns ArrayList<ArrayList<Integer>> adjacArray = new ArrayList<ArrayList<Integer>>(); for(int i=0; i<SubColumns.size(); i++) adjacArray.add(new ArrayList<Integer>()); // We generate the solution's ArrayList ArrayList<ArrayList<Integer>> solution = new ArrayList<ArrayList<Integer>>(); // We generate a temporary ArrayList that keeps positions that covers the best subarraylist ArrayList<Integer> coveredPositions = new ArrayList<Integer>(); for(int i=0; i<SubColumns.size(); i++) { for(int j=0; j<Columns.size(); j++) { if(isContained(SubColumns.get(i),Columns.get(j))) adjacArray.get(i).add(1); else adjacArray.get(i).add(0); } } // So far, we have generated an array full with 0 and 1 that shows which subarraylist covers which arraylist // Generate a list that keeps count of how many arraylists are covered by each subarraylist ArrayList<Integer> sumOfCovers = new ArrayList<Integer>(); for(int i=0; i<adjacArray.size(); i++) sumOfCovers.add(0); int sum = 0; for(int i=0; i<adjacArray.size(); i++) { sum = 0; for(int j=0; j<adjacArray.get(i).size(); j++) { sum = sum + adjacArray.get(i).get(j); } sumOfCovers.set(i,sum); } boolean empty = false; while(!empty) { int max = Collections.max(sumOfCovers); int position = 0; // find the position with the max cover for(int i=0; i<sumOfCovers.size(); i++) { if(max == sumOfCovers.get(i)){ position = i; break; } } // Add it to the solution solution.add(SubColumns.get(position)); // Update adjacArray for(int i=0;i<adjacArray.get(position).size(); i++) { if(adjacArray.get(position).get(i) == 1) coveredPositions.add(i); } for(int i=0; i<adjacArray.size(); i++) { for(int j=0; j<coveredPositions.size(); j++){ adjacArray.get(i).set(coveredPositions.get(j), 0); sumOfCovers.set(i, sumOfCovers.get(i) - 1); } } coveredPositions.clear(); // empty to reuse it if(Collections.max(sumOfCovers) == 0) // if all arraylists are covered empty = true; // exit } return solution; }

This post has been edited by **Larry71**: 15 June 2019 - 12:42 PM

Page 1 of 1