JVM 内存配置参数

一个重要的概念就是我们常常所说的设置jvm的内存参数大多关注的是堆内存的大小。
简单理解成这样:堆内存 = Old + Eden + S0 + S1 ;年轻的 = Eden(新生代) + S0 + S1 。

栈内存大小相关设置
-Xss1024k
意义: 设置线程栈占用内存大小。
默认值:不同的操作系统平台,其默认值不同。

堆内存大小相关设置

-Xms512m
意义: 设置堆内存初始值大小。
默认值:如果未设置,初始值将是老年代和年轻代分配制内存之和。

-Xmx1024m
意义: 设置堆内存最大值。
默认值:不同的操作系统平台,其默认值不同, 具体请查看官网 或者 查看讨论 How is the default Java heap size determined?
年轻代内存大小相关设置

-Xmn512m
意义: 设置新生代的初始值及最大值。
默认值:堆内存的1/4(这里要记住不是最大堆内存,还是已经分配的堆内存的1/4)。

-XX:NewSize=512m
意义:设置新生代的初始值。

-XX:MaxNewSize=512m
意义:设置新生代的最大值。

比率方式设置

-XX:NewRatio=8
意义:设置老年代和年轻代的比例。比如:-XX:NewRatio=8 表示老年代内存:年轻代内存=8:1 => 老年代占堆内存的8/9,年轻代占堆内存的1/9。
默认值:2

-XX:SurvivorRatio=32
意义:设置新生代和存活区的比例(这里需要注意的是存活区指的是其中一个)。比如:-XX:SurvivorRatio=8 表示存活区:新生代=1:8 => 新生代占年轻代的8/10,每个存活区各占年轻代的1/10。
默认值:8

-XX:MinHeapFreeRatio=40
意义:GC后,如果发现空闲堆内存占到整个预估上限值的40%,则增大上限值。
默认值:40

-XX:MaxHeapFreeRatio=70
意义:GC后,如果发现空闲堆内存占到整个预估上限值的70%,则收缩预估上限值。
默认值:70

Meta大小相关设置

-XX:MetaspaceSize=128m
意义:初始元空间大小,达到该值就会触发垃圾收集进行类型卸载,同时GC会对该值进行调整:如果释放了大量的空间,就适当降低该值;如果释放了很少的空间,那么在不超过MaxMetaspaceSize时,适当提高该值。
默认值:依赖平台

-XX:MaxMetaspaceSize=256m
意义:设置元空间的最大值,默认是没有上限的,也就是说你的系统内存上限是多少它就是多少
默认值:默认没有上限,在技术上,Metaspace的尺寸可以增长到交换空间

参考链接

Java HotSpot VM Options

Java Platform, Standard Edition Tools Reference

How does HttpAsyncClient 4 work?

By default HttpAsyncClient permits only two concurrent connections to the same host per RFC 2616 specification. This limit has nothing to do with the number of i/o dispatch threads used internally by the i/o reactor.

The code above will create two outgoing connections at most.

HTTP message pipelining has nothing do with connection persistence per se, though pipelined request execution implies the use of persistent connections.

HTTP pipelining is about message sequencing. HttpAsyncClient in the pipelining mode can send multiple requests without waiting for each response.

Default mode:

C -> request1 -> S
C <- response1 <- S
C -> request2 -> S
C <- response2 <- S

Pipelining mode:

C -> request1 -> S
C -> request2 -> S
C <- response1 <- S
C <- response2 <- S

Apache HttpAsyncClient 源码分析

Maven中的依赖作用范围(scope)

scope取值 有效范围(compile, runtime, test) 依赖传递 例子
compile all spring-core
provided compile, test servlet-api
runtime runtime, test JDBC驱动
test test JUnit
system compile, test

final vs static final in java

For final, it can be assigned different values at runtime when initialized. For example

Class Test{
  public final int a;
}
Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t2.a = 20; //fixed

Thus each instance has different value of field a.

For static final, all instances share the same value, and can’t be altered after first initialized.

Class TestStatic{
      public static final int a;
}
Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t1.a = 20;   // ERROR, CAN'T BE ALTERED AFTER THE FIRST INITIALIZATION.

private final static attribute vs private final attribute
Static vs Instance Variables: Difference?

Spring RestTemplate 消息转换2种使用方式

拦截器

public class MyXmlInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    ClientHttpResponse response = execution.execute(request, body);
    HttpHeaders headers = response.getHeaders();

    // you'd want to check if the value needs to be changed
    if (headers.containsKey("Content-Type")) {
        headers.remove("Content-Type");
    }

    headers.add("Content-Type", "application/xml");

    return response;
}
RestTemplate t = new RestTemplate();
t.getInterceptors().add(new MyXmlInterceptor());

自定义

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_HTML);
jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(jaxbMessageConverter);
restTemplate.setMessageConverters(messageConverters);

Unsafe.park & Object.wait

如果是搞应用程序的程序员,就不应该使用这两种方法中的任何一种。两种都太底层了,建议使用 java.util.concurrent.locks , 毕竟类库的就是让你多快好省的写代码。

park 直接操作线程,线程作为参数直接传入,线程会sleep知道调用了 unpark 方法。这种操作需要明确知道那个线程需要 block/unblock。可以使用 java.util.concurrent.locks.LockSupport 完成操作。

park 操作可以理解为Thread.sleep 操作,不会释放锁资源,使用不当会造成死锁。park 操作能够设置一个 blocker,并通过 getBlocker 获取阻塞的信息,方便做一些调试之类的工作。

wait 操作会释放锁。wait 操作必须在同步语句块。如果不在同步语句块会抛出 IllegalMonitorStateException 异常。使用 notify 和 notifyAll 唤醒线程。

wait用于锁机制,sleep不是,这就是为啥sleep不释放锁,wait释放锁的原因,sleep是线程的方法,跟锁没半毛钱关系,wait,notify,notifyall 都是Object对象的方法,是一起使用的,用于锁机制

It all eventually makes its way down to the OS’s scheduler, which hands out timeslices to processes and threads.

sleep(n) says “I’m done with my timeslice, and please don’t give me another one for at least n milliseconds.” The OS doesn’t even try to schedule the sleeping thread until requested time has passed.

yield() says “I’m done with my timeslice, but I still have work to do.” The OS is free to immediately give the thread another timeslice, or to give some other thread or process the CPU the yielding thread just gave up.

wait() says “I’m done with my timeslice. Don’t give me another timeslice until someone calls notify().” As with sleep(), the OS won’t even try to schedule your task unless someone calls notify() (or one of a few other wakeup scenarios occurs).

Threads also lose the remainder of their timeslice when they perform blocking IO and under a few other circumstances. If a thread works through the entire timeslice, the OS forcibly takes control roughly as if yield() had been called, so that other processes can run.

You rarely need yield(), but if you have a compute-heavy app with logical task boundaries, inserting a yield() might improve system responsiveness (at the expense of time — context switches, even just to the OS and back, aren’t free). Measure and test against goals you care about, as always.

继续阅读“Unsafe.park & Object.wait”