Tutorial Addendum On Java - Beheading Ambiance
| |
The program seems to be confusing:
- Why do I charge two altered methods to retrieve the anamnesis information?
- Why are the methods alleged again 5 times?
- Why are the press statements for the getMemoryInfo() adjustment not following
the adjustment alarm statements immediately?
Answers to these questions are in the achievement of the program:
Print anamnesis advice - move = 0
Chargeless anamnesis = 1777568
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 254048
Print anamnesis advice - move = 1
Chargeless anamnesis = 1774712
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 256904
Print anamnesis advice - move = 2
Chargeless anamnesis = 1772496
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 259120
Print anamnesis advice - move = 3
Chargeless anamnesis = 1770280
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 261336
Print anamnesis advice - move = 4
Chargeless anamnesis = 1768064
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 263552
Get anamnesis advice - move = 0
Chargeless anamnesis = 1777568
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 254048
Get anamnesis advice - move = 1
Chargeless anamnesis = 1777568
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 254048
Get anamnesis advice - move = 2
Chargeless anamnesis = 1777568
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 254048
Get anamnesis advice - move = 3
Chargeless anamnesis = 1777568
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 254048
Get anamnesis advice - move = 4
Chargeless anamnesis = 1777568
Absolute anamnesis = 2031616
Best anamnesis = 134217728
Anamnesis acclimated = 254048
- The aberration amid the getMemoryInfo() adjustment and printMemoryInfo() adjustment is
that the getMemoryInfo() adjustment saves the anamnesis acceptance into arrays, while
printMemoryInfo() adjustment prints the anamnesis acceptance ethics on the console. This is to show
the appulse of the press statements, System.out.println(), on the anamnesis usage.
- Calling the methods again is to see if there are any cilia additional than
the appliance program cilia active that ability affect the anamnesis usage. As you
can see from the output, the anamnesis acceptance was un-changed during the 5 calls to
getMomeryInfo(). However, afterwards anniversary alarm to printMemoryInfo(), the chargeless anamnesis decreased
by added than 2000 bytes. This suggests that the System.out.println() adjustment was leaving
dead altar in anamnesis for the debris beneficiary to abolish them.
- Calling the getMemoryInfo() at the alpha and putting the accompanying printing
statements at the end is to abstain any appulse of press statements on the printMemoryInfo()
calls.
Calculating Anamnesis Acceptance of an Array
The anamnesis infomation provided by the JVM can be acclimated to account the anamnesis usage
of data altar acclimated in the appliance program. In the afterward program, I approved
to account the anamnesis acceptance of an array:
/**
* MemoryUsage.java
* Absorb (c) 2002 by Dr. Yang
*/
public chic MemoryUsage {
clandestine changeless long fm = new long;
clandestine changeless long tm = new long;
clandestine changeless long mm = new long;
clandestine changeless long um = new long;
accessible changeless abandoned main(String a) {
Runtime rt = Runtime.getRuntime();
getMemoryInfo(rt,0);
long la = null;
continued s = allocateLongArray(la);
getMemoryInfo(rt,1);
System.out.println("Memory acceptance afore arrangement allocation:");
System.out.println(" Chargeless anamnesis = "+fm);
System.out.println(" Absolute anamnesis = "+tm);
System.out.println(" Best anamnesis = "+mm);
System.out.println(" Anamnesis acclimated = "+um);
System.out.println("Memory acceptance afterwards arrangement allocation:");
System.out.println(" Chargeless anamnesis = "+fm);
System.out.println(" Absolute anamnesis = "+tm);
System.out.println(" Best anamnesis = "+mm);
System.out.println(" Anamnesis acclimated = "+um);
System.out.println("Memory acceptance summary:");
System.out.println(" Allocated to the arrangement = "+s);
System.out.println(" Anamnesis acceptance access = "+(um-um));
System.out.println(" Aerial of individual arrangement = "
+(um-um-s));
}
accessible changeless abandoned getMemoryInfo(Runtime rt, int i) {
fm = rt.freeMemory();
tm = rt.totalMemory();
mm = rt.maxMemory();
um = tm-fm;
}
accessible changeless continued getMemoryInfo(Runtime rt) {
//rt.gc();
System.out.println("Getting JVM anamnesis infomation...");
continued fm = rt.freeMemory();
continued tm = rt.totalMemory();
continued mm = rt.maxMemory();
System.out.println(" Chargeless anamnesis = "+fm);
System.out.println(" Absolute anamnesis = "+tm);
System.out.println(" Best anamnesis = "+mm);
System.out.println(" Anamnesis acclimated = "+(tm-fm));
acknowledgment tm-fm;
}
accessible changeless continued allocateLongArray(long la) {
int s = 2*128*1024;
la = new long; // 2 MB bare for this array
acknowledgment 8*s;
}
}
|
memory, system, println, total, maximum, usage, static, getmemoryinfo, method, array, public, program, runtime, private, 254048get, printmemoryinfo, statements, methods, printing, , system out, free memory, maximum memory, total memory, memory used, memory usage, 2031616 maximum, memory 134217728, 134217728 memory, memory 2031616, memory info, info step, println memory, memory 1777568, 1777568 total, static long, new long, public static, 254048get memory, used 254048get, private static, println maximum, println total, getmemoryinfo method, printing statements, println free, 2031616 maximum memory, maximum memory 134217728, memory 134217728 memory, 134217728 memory used, total memory 2031616, memory 2031616 maximum, memory info step, memory 1777568 total, free memory 1777568, 1777568 total memory, private static long, println memory usage, 254048get memory info, memory used 254048get, used 254048get memory, println total memory, println memory used, println maximum memory, println free memory, public static long, public static void, java execution environment, array allocation system, |
Also see ...
Output: D:\write_20021217java_20021217semoryUsageMemory acceptance afore arrangement allocation: Chargeless anamnesis = 1777248 Absolute anamnesis = 2031616 Best anamnesis =
Execution ProcessA Java appliance program is create of one or added classes and aught or added interfaces. One of the chic musthave the main() adjustment as the beheading star
Execution Access PointAs mentioned in the antecedent section, a Java appliance program haveto accept a starting chic with a appropriate adjustment alarm main() as the beheading acces
Execution ConsoleWhen the JVM is active a Java appliance program, it needs a animate windowto provides 3 predefined ascribe and achievement streams to the Java program: to:br
Why SynchronizationThe better problem of acceptance assorted accoutrement administration the aforementioned data setis that one operation in one cilia could bang with addition operation
Synchronization Abutment in JavaInstead of let the programmers to architecture their own locks, administer the synchronization blocks, and administer the synchronization rules, Java
Synchronization Sample ProgramNow, let s address a Java program to see how the synchronization techniquecan break the coffer problem. Two classes are advised to simulate a bankp
clandestine changeless abandoned printResult() { System.out.print("Account"); for (int i=0; i<t_maxi; i++) System.out.print(", ATM "+i); System.out.print(", Transaction Sum, Balance"); for (int j=0; j<
Output: No synchronization:Account, ATM 0, ATM 1, ATM 2, ATM 3, ATM 4, Transaction Sum, Balance0, 506.81, 227.06, 286.89, 885.14, 330.11, 465.73, 1051.871, 63.98, 27.74, 413.05, 276.93, 86.81,
Output: Hello world! From the capital program.Hello world! From a thread.Note that:The program behaves the aforementioned way as the antecedent p