Gurgaon Jobs, Companies and Consultants

Those who are looking for job in Gurgaon can visit this site : Gurgaon Jobs. Here you will find email ids/contact no of "Companies in Gurgaon" and also of "Placement Consultants in Gurgaon"

Friday, June 19, 2009

Java Memory

Some good references to understand java memory problems.

Read BEST practices to avoid memory leaks

http://jb2works.com/memoryleak/topfive.html

Memory leaks with tools to diagonise :

http://jb2works.com/memoryleak/index.html

Java Memory Puzzle Eye Opener

http://mattfleming.com/node/306


My Memory Related Experiments :

Experiment 1 :

Enviornment : Processor=2.80 Memory=1.93GB(2072321024 bytes) RAM

Case 1 : I ran a program and printed memory using Runtime.getRuntime().maxMemory() Output : 66650112 bytes OR 63.5625 MB

Case 2 : Then i passed argument -Xmx512m and printed memory Runtime.getRuntime().maxMemory()
Output : 532742144 bytes OR 508.0625 MB

Case 3 : Then i passed argument -Xmx1200m and printed memory Runtime.getRuntime().maxMemory()
Output : 1248657408 bytes OR 1190.8125 MB OR 1.16GB

Case 4 : Then i passed argument -Xmx1612m and printed memory Runtime.getRuntime().maxMemory()
Output : 1677328384 bytes OR 1599.625 MB OR 1.56GB

But when i tried to give 1613 or more it started giving error :
Error occurred during initialization of VM Could not reserve enough space for object heap

Experiment 2 :
Case 1 :

public class Test {

public static void ppp1() {
byte[] test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
}
public static void ppp2() {
byte[] test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
}
public static void main(String args[]) {
ppp1();
ppp2();
}
}
O/P : Works Properly

Case 2 :

public class Test {

public static void ppp1() {
byte[] test1 = null;
byte[] test2 = null;
try {
test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
try {
test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}
O/P : Gives Out of Memory
Case 3 :

public class Test {

public static void ppp1() {
byte[] test1 = null;
byte[] test2 = null;
try {
test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
test1 = null;
try {
test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}
O/P : Works Properly
Case 4 :

public class Test {

public static void ppp1() {
byte[] test1 = null;
byte[] test2 = null;
byte[] test3 = null;
try {
test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
test3 = test1;
} catch(Exception e) {
e.printStackTrace();
}
test1 = null;
try {
test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}
O/P : Gives Out of Memory

Case 5 :

public class Test {

public static void ppp1() {
byte[] test1 = null;
byte[] test2 = null;
byte[] test3 = null;
try {
test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
test3 = test1;
} catch(Exception e) {
e.printStackTrace();
}
test1 = null;
test3 = null;
try {
test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}
O/P : Works Properly


Experiment 3 :
Case 1 :

public class Test {

public static void ppp1() {
byte[] test1 = null;
byte[] test2 = null;
try {
test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
try {
test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}


O/P :
Gives Out of Memory

Case 2 :

public class Test {

public static void ppp1() {
byte[] test1 = null;
byte[] test2 = null;
try {
test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
for(int i=0; i< 100000; i++) {

}
try {
test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}
O/P : Works Properly. For index condition i less than 100000 it was giving Out of memory.
Case 3 :

public class Test {

public static void ppp1() {
try {
byte[] test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
try {
byte[] test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}

O/P : Gives Out of Memory
Case 4 :

public class Test {

public static void ppp1() {
try {
byte[] test1 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
for(int i=0; i < 0; i++) {

}
try {
byte[] test2 = new byte[(int)(Runtime.getRuntime().maxMemory()/2)];
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
ppp1();
}
}

O/P : Works Properly for any condition i less than 0, 1, 2 .. it was working.

No comments: