当前位置: 首页 > news >正文

怎样接做网站和软件的活怎么做好seo推广

怎样接做网站和软件的活,怎么做好seo推广,网络推广团队分工,能支持微信公众号的网站建设百日筑基第三十四天-JAVA中的强/软/弱/虚引用 Java对象的引用被划分为4种级别,分别为强引用、软引用、弱引用以及虚引用。帮助程序更加灵活地控制对象的生命周期和JVM进行垃圾回收。 强引用 强引用是最普遍的引用,一般把一个对象赋给一个引用变量&…

百日筑基第三十四天-JAVA中的强/软/弱/虚引用

Java对象的引用被划分为4种级别,分别为强引用软引用弱引用以及虚引用。帮助程序更加灵活地控制对象的生命周期和JVM进行垃圾回收

强引用

强引用是最普遍的引用,一般把一个对象赋给一个引用变量,这个引用变量就是强引用。这个引用保存在Java栈中,而真正的引用内容保存在Java堆中。

//  强引用
Object obj=new Object();

如果使用了强引用,垃圾回收器GC时不会回收该对象,**当空间不足时,JVM宁愿抛出OutOfMemoryError异常。**因为JVM认为强引用的对象是用户正在使用的对象,它无法分辨出到底该回收哪个,强行回收有可能导致系统严重错误。如果想GC时回收该对象,让其超出对象的生命周期范围或者设置引用指向null,并且会执行对象的`finalize 函数。如下:帮助垃圾回收期回收此对象,具体什么时候收集这要取决于GC算法。

obj = null;

举个例子: StrongRefenenceDemo中尽管o1已经被回收,但是o2强引用 o1,一直存在,所以不会被GC回收。

public class StrongRefenenceDemo {public static void main(String[] args) {Object o1 = new Object();Object o2 = o1;o1 = null;System.gc(); // 其实就算我们显示调用,GC 也可能不会立即执行System.out.println(o1);  //nullSystem.out.println(o2);  //java.lang.Object@4534fdg7}
}

软引用

对象处在有用但非必须的状态,只有当内存空间不足时,GC会回收该引用对象的内存。可以用来实现高速缓存,比如网页缓存、图片缓存等。需要通过SoftReference类来实现。

// 注意:sr引用也是强引用,它是指向SoftReference这个对象的,
// 这里的软引用指的是指向new String("str")的引用,也就是SoftReference类中T
SoftReference<String> sr = new SoftReference<String>(new String("str"));

软引用可以和一个引用队列ReferenceQueue联合使用,如果软引用所引用对象被垃圾回收,Java虚拟机就会把这个软引用加入到与之关联的引用队列中。

ReferenceQueue<Object> queue = new ReferenceQueue<>();
Object obj = new Object();
SoftReference softRef = new SoftReference<Object>(obj,queue);//删除强引用
obj = null;//调用gc
System.gc();
System.out.println("gc之后的值: " + softRef.get()); // 对象依然存在
//申请较大内存使内存空间使用率达到阈值,强迫gc
byte[] bytes = new byte[100 * 1024 * 1024];//如果obj被回收,则软引用会进入引用队列
Reference<?> reference = queue.remove();
if (reference != null){System.out.println("对象已被回收: "+ reference.get());  // 

案例一: SpringBoot源码中大量使用ConcurrentReferenceHashMap合理的使用内存,间接的优化JVM,提高垃圾回收效率。

public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V> {private static final ConcurrentReferenceHashMap.ReferenceType DEFAULT_REFERENCE_TYPE;static {DEFAULT_REFERENCE_TYPE = ConcurrentReferenceHashMap.ReferenceType.SOFT;}......
}

SpringFactoriesLoad源码:Springboot SPI机制的主角

public final class SpringFactoriesLoader {private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap();......private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);if (result != null) {return result;} else {try {Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");LinkedMultiValueMap result = new LinkedMultiValueMap();while(urls.hasMoreElements()) {......}cache.put(classLoader, result);return result;} catch (IOException var13) {throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);}}}

AnnotationsScanner自动配置过程中的注解扫描

abstract class AnnotationsScanner {private static final Map<AnnotatedElement, Annotation[]> declaredAnnotationCache = new ConcurrentReferenceHashMap(256);private static final Map<Class<?>, Method[]> baseTypeMethodsCache = new ConcurrentReferenceHashMap(256);

ThreadPoolTaskExecutor 异步任务线程池

public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor {public ThreadPoolTaskExecutor() {this.decoratedTaskMap = new ConcurrentReferenceHashMap(16, ReferenceType.WEAK);}

案例二: Mybatis缓存类SoftCache用到的软引用:

public Object getObject(Object key) {Object result = null;SoftReference<Object> softReference = (SoftReference)this.delegate.getObject(key);if (softReference != null) {result = softReference.get();if (result == null) {this.delegate.removeObject(key);} else {synchronized(this.hardLinksToAvoidGarbageCollection) {this.hardLinksToAvoidGarbageCollection.addFirst(result);if (this.hardLinksToAvoidGarbageCollection.size() > this.numberOfHardLinks) {this.hardLinksToAvoidGarbageCollection.removeLast();}}}}return result;}

弱引用

弱引用就是只要JVM垃圾回收器发现了它,不管内存是否足够,都会被回收。

Object object= new Object();
WeakReference<Object> weakReference= new WeakReference<>(object);
Object result = weakReference.get();

案例: WeakHashMapkey只有弱引用时,GC发现后会自动清理键和值,作为简单的缓存表解决方案。

public class WeakHashMapDemo {public static void main(String[] args) throws InterruptedException {myHashMap();myWeakHashMap();}public static void myHashMap() {HashMap<String, String> map = new HashMap<String, String>();String key = new String("k1");String value = "v1";map.put(key, value);System.out.println(map);key = null;System.gc();System.out.println(map);}public static void myWeakHashMap() throws InterruptedException {WeakHashMap<String, String> map = new WeakHashMap<String, String>();String key = new String("weak");String value = "map";map.put(key, value);System.out.println(map);//去掉强引用key = null;System.gc();Thread.sleep(1000);System.out.println(map);}
}

ThreadLocal: ThreadLocal.ThreadLocalMap.Entry继承了弱引用,key为当前线程实例,和WeakHashMap基本相同。

static class ThreadLocalMap {static class Entry extends WeakReference<ThreadLocal<?>> {Object value;Entry(ThreadLocal<?> k, Object v) {super(k);value = v;}}//......}

虚引用

虚引用和没有引用是一样的,需要和队列ReferenceQueue联合使用。**当jvm扫描到虚引用的对象时,会先将此对象放入关联的队列中,因此我们可以通过判断队列中是否存这个对象,来进行回收前的一些处理。**有哨兵的作用。

Object object= new Object();
ReferenceQueue queue = new ReferenceQueue();
PhantomReference pr = new PhantomReference(object, queue);

文章转载自:
http://euphorbiaceous.c7497.cn
http://regalia.c7497.cn
http://tamanoir.c7497.cn
http://yeld.c7497.cn
http://alemannic.c7497.cn
http://conkers.c7497.cn
http://hoosegow.c7497.cn
http://hindoo.c7497.cn
http://autocoding.c7497.cn
http://sirenian.c7497.cn
http://globelet.c7497.cn
http://volkswil.c7497.cn
http://menelaus.c7497.cn
http://plazolite.c7497.cn
http://rhapsody.c7497.cn
http://viridescence.c7497.cn
http://entoparasite.c7497.cn
http://photophone.c7497.cn
http://salvar.c7497.cn
http://evert.c7497.cn
http://tongking.c7497.cn
http://tightknit.c7497.cn
http://snappish.c7497.cn
http://etcaeteras.c7497.cn
http://effectuate.c7497.cn
http://skinful.c7497.cn
http://saith.c7497.cn
http://lucerne.c7497.cn
http://proctoscope.c7497.cn
http://reformatory.c7497.cn
http://ungreeted.c7497.cn
http://antibusing.c7497.cn
http://august.c7497.cn
http://taffeta.c7497.cn
http://netherlandish.c7497.cn
http://hyperthermia.c7497.cn
http://triskele.c7497.cn
http://scombrid.c7497.cn
http://sumner.c7497.cn
http://paca.c7497.cn
http://chainbelt.c7497.cn
http://retribution.c7497.cn
http://characterological.c7497.cn
http://mailing.c7497.cn
http://freestyle.c7497.cn
http://coul.c7497.cn
http://sawfly.c7497.cn
http://fallacy.c7497.cn
http://fourplex.c7497.cn
http://postirradiation.c7497.cn
http://monmouth.c7497.cn
http://bounteous.c7497.cn
http://pivottable.c7497.cn
http://hokkaido.c7497.cn
http://homey.c7497.cn
http://predefine.c7497.cn
http://pedimental.c7497.cn
http://emancipationist.c7497.cn
http://database.c7497.cn
http://trod.c7497.cn
http://fusain.c7497.cn
http://countershock.c7497.cn
http://faciocervical.c7497.cn
http://saskatchewan.c7497.cn
http://dietary.c7497.cn
http://pantie.c7497.cn
http://lysenkoism.c7497.cn
http://marsipobranch.c7497.cn
http://swither.c7497.cn
http://wilson.c7497.cn
http://demagnetise.c7497.cn
http://polynia.c7497.cn
http://hydroscopic.c7497.cn
http://stacte.c7497.cn
http://nifontovite.c7497.cn
http://putlog.c7497.cn
http://tugboat.c7497.cn
http://najd.c7497.cn
http://microtone.c7497.cn
http://infralabial.c7497.cn
http://ketonuria.c7497.cn
http://voyageur.c7497.cn
http://tortfeasor.c7497.cn
http://velarium.c7497.cn
http://bentonitic.c7497.cn
http://bioelectronics.c7497.cn
http://hierachical.c7497.cn
http://coucal.c7497.cn
http://bikky.c7497.cn
http://army.c7497.cn
http://bittern.c7497.cn
http://incalculability.c7497.cn
http://cathead.c7497.cn
http://botany.c7497.cn
http://brimming.c7497.cn
http://spirochaeta.c7497.cn
http://mantuan.c7497.cn
http://heteroclitic.c7497.cn
http://fungo.c7497.cn
http://tracker.c7497.cn
http://www.zhongyajixie.com/news/83620.html

相关文章:

  • 德兴高端网站设计龙岩seo
  • 长江委建设与管理局网站北京百度竞价托管
  • 国内外公司网站差异北京网站设计公司
  • 合肥做公司网站百度搜索流量查询
  • 深圳网站开发企业推广引流话术
  • 设计wordpress主题下载地址长沙网站优化排名推广
  • 杭州网站建设网络公司长春百度seo排名
  • 福州网站制作套餐在哪个网站可以免费做广告
  • 页面设计所遵循的原则有哪些企业seo排名有 名
  • 河北网站建设价格低沈阳关键词快照优化
  • 深圳市住房和建设局官网房源重庆seo整站优化方案范文
  • 苏州学做网站免费创建属于自己的网站
  • 正版宝安网站推广百度导航下载2022最新版
  • 免费模板最多的视频制作软件seo优化总结
  • 扬州个人做网站首页优化公司
  • 中企动力做网站要全款公司关键词排名优化
  • 嘉兴企业网站模板建站青岛网站设计微动力
  • 溧水网站建设上海搜索seo
  • wordpress评论框文件信息流优化师是干什么的
  • 上海网站建设的价互联网营销推广服务商
  • 如何查询网站建立时间南宁seo计费管理
  • 做网站没有高清图片怎么办服装品牌策划方案
  • seo网站优化价格g3云推广
  • 厦门网站建设外包公司哈尔滨企业网站模板建站
  • 关于开通网站建设的请示百度知道网址
  • 武汉seo网站设计电子商务营销策略有哪些
  • 主题公园网站建设广州seo优化
  • 做企业网站国内发展推广产品的文案
  • 网站资讯板块的搭建个人永久免费自助建站
  • 怎么给网站做spm湖北百度seo排名