![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Reduce Memory Consumption
Hey i was wondering if there was a command for reducing the memory consumption, or CPU usage of a java program.
I dont know if this is typical of java programs, but im pushing around 20,000K just writing text to a file. is this normal?, is it just my computer?, or something wrong in the code maybe? heres some of the code: (its the only loop) BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
Random rand = new Random();
for (int line = 0; line <= MAX; line++)
{
for (int num = 0; num <= MAX; num++)
{
intValue = rand.nextInt(90) + (10);
bw.write(intValue + " ");
}
bw.newLine();
}
__________________
Hoes telling me to calm down but I'm like fuck that shit!
|
|
|
|
|
|
#2 |
|
Professional Programmer
|
It's not pushing 20,000K because of you're writing to a text file. It's pushing 20,000K because of your nested for loops.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
well i kinda need a nested for loop to make a square type output...is there anyway to reduce the memory consumption?
__________________
Hoes telling me to calm down but I'm like fuck that shit!
|
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I believe that this is normal because Java programs run on Java virtual machine instead of running natively on windows. Not sure though.
BTW, for loops don't fill up memory, they use up CPU power. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
The memory usage isn't directly related to the nested loop (except for the fact that you're writing (MAX+1)*(MAX+1) integer values with separating whitespace to a file).
The memory usage is probably related to the fact that you're using a BufferedWriter. A BufferedWriter stores data (i.e. buffers it) in memory until it is flushed (i.e. physically written) to the file. That buffering can consume memory. Writing directly to the FileWriter will probably eliminate the buffering (and hence the memory usage), but the trade-off will be I/O performance of your program. The disk (or other I/O device) is usually an I/O bottleneck for any program, and slowing that I/O down tends to have a very noticeable effect on how fast a file is written. A rough rule of thumb is that physically writing lots of little chunks to disk will be slower than writing a few big chunks. The whole purpose of buffered I/O is using memory to store data temporarily until bigger chunks are available to be written. The amount of memory consumed depends on how the buffering is implemented but most good-quality schemes will work within available memory resources. I'm not sure offhand how good the scheme used by BufferedWriter is. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
I would assume 20 000k is well within 'available resources'
![]() |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2005
Posts: 29
Rep Power: 0
![]() |
I don't know if this would help, but you should close the reader after you are done using it.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|