Created
March 9, 2015 08:15
-
-
Save tgkprog/37b9d7e6509091238b54 to your computer and use it in GitHub Desktop.
Memory - function lcoals - get cleaned up?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.concurrent.Executor; | |
import java.util.concurrent.Executors; | |
/** | |
Run with param 1 and 2 or change type to "2" and "1" | |
Available memory changes, used jconsole to change force garbage collection. | |
See video https://www.youtube.com/watch?v=MadBdryX8uk | |
*/ | |
public class ObjectLife { | |
public static void main(String[] args) { | |
// simple multi thread to mimic web app | |
// ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 3, 110, null, null); | |
Executor pool = Executors.newFixedThreadPool(3); | |
String type = "1"; | |
if (args.length > 0) { | |
type = args[0]; | |
} | |
Work w = null; | |
if ("1".equals(type)) { | |
w = new Worker(); | |
} else { | |
w = new Worker2(); | |
} | |
w.init(2); | |
System.out.println("w type " + w.getClass().getName()); | |
Watch.me.print(); | |
pool.execute(w); | |
pool.execute(Watch.me); | |
} | |
} | |
class Watch implements Runnable { | |
long prev = 0; | |
static Watch me = new Watch(); | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
Thread.sleep(1100); | |
} catch (InterruptedException e) { | |
System.out.println("Intrpt thread " + e); | |
} | |
print(); | |
} | |
} | |
public void print() { | |
Runtime r = Runtime.getRuntime(); | |
long free = r.freeMemory(); | |
System.out.println("Free " + free + ", delta " + (free - prev)); | |
System.out.println(", av " + r.maxMemory()); | |
prev = free; | |
} | |
} | |
class Work implements Runnable { | |
double val = 0; | |
double val3 = 0; | |
public void init(double val) { | |
this.val = val; | |
} | |
void do2() { | |
} | |
@Override | |
public void run() { | |
int cnt = 0; | |
while (++cnt < 175) { | |
do2(); | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
System.out.println("Intrpt thread " + e); | |
} | |
} | |
System.gc(); | |
System.out.println("Type, v3, : " + this.getClass().getName()); | |
Watch.me.print(); | |
} | |
} | |
class Worker extends Work { | |
public void do2() { | |
double local = ++val; | |
double local2 = local * 2; | |
System.out.println(" local " + local + " double " + local2); | |
val3 = local2 + 1; | |
} | |
} | |
class Worker2 extends Work { | |
public void do2() { | |
System.out.println(" local " + ++val + " double " + (val * 2)); | |
val3 = (val * 2) + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment