How can I fix memory leaks? (2024)

  • jmap and built-in dumpers in JDK
    • Obtain the dump.
    • Analyze the problem.
  • Other tools
  • Tips and tricks
    • Common leaking objects
    • Leaks vs. retained memory

The first problem is to identify what is the root problem causing memory to not be used effectively.The usual approach for this is to analyze the complete contents of memory when the problem appears, using one of a number of appropriate tools, and ideally then find a solution.

Below are some hints on how to analyze the content of memory:

jmap and built-in dumpers in JDK

Obtain the dump.

If the problem causes OutOfMemoryError, it is possible to customize the JVM to provide a memory dump automatically whenever an OutOfMemoryError is thrown.FaqNetBeansAndOOME describes what options can be used for this.If you are developing modules, it is a very good idea to set the option -J-XX:+HeapDumpOnOutOfMemoryError.

If the memory leak is not so aggresive to fill all the available memory and cause an OutOfMemoryError, it is still possible to use jmap to generate the same dump.Running full GC before you create this dump can be a good idea as it can strip the size of dump file and remove some unimportant objects from the snapshot.You can do this by turning memory toolbar on (do a right click in toolbar area and check Memory).Repeating this several times can even collect large amounts of data held in various caches through soft or weak references and make it easier to browse the dump.

Analyze the problem.

Once you have the dump of the heap in a file, it is possible to open it usingthe NetBeans profiler.This has a number of analysis features and is integrated with the IDE, e.g. to browse sources.

Alternately, you can use the JDK’s tool jhat.It will start simple web server and you can use a web browser to see the data.There are many functions starting with lists of classes with numbers of objects and their size, navigation between references,finding of reference chains from GC root to certain objects.JavaScript can be used to express more complex queries.

INSANE is a home-grown tool that is useful for analysis of memory content and also can be used in automated tests - so once you have fixed a memory leak, you can write a test that will fail if the memory leak is ever recreated.NbTestCase.assertGC is all you need to know. See also FitnessMemoryLeaks.

Timers/counters module can be used to register objects of interest in the code, then inspect them during IDE run via Runtime Watches window.

Advanced: DTrace can be used to monitor object allocation and garbage collection. Nice article about using DTrace with the HotSpot provider: Java and DTrace.

Tips and tricks

Common leaking objects

There are some typical classes where it should be easily possible to tellwhat the appropriate number of their instances in memory should be,and if these are leaking there is a serious problem:

  • Projects - it means instances of all subclasses of org.netbeans.api.project.Project

  • Editors (or TopComponent`s) - it can be useful to check for `org.openide.text.QuietEditorPane instances to see if closed editors can release substantial part of associated memory. If the editor component is held it often means that associated editor support is held too linking to parsing data, sidebars providing versioning information and probably also project metadata. It is also possible to look for instance of org.openide.windows.TopComponent if there is some suspicion or better to search for its particular subclasses. Generally there will be always certain numbers of `TopComponent`s.

  • Documents - somewhat related to editors. An important class where you can start is org.netbeans.modules.editor.NbEditorDocument.

  • Top-level windows - undisposed dialogs can be a problem as these hold native resources that can be limited in the system.

  • ClassLoader - we need to be very careful and check that class loaders created dynamically during runtime can be GC’ed when they are no longer used. Without this the result is OOME signaling that perm gen area is full.

  • CompilationInfo (java.source module) - related to Java infrastructure. An important class where you can start is com.sun.tools.javac.code.Symtab, which is a singleton in a javac instance.

Leaks vs. retained memory

There are two different ways how memory can be wasted: leaks and improper retention of memory.

Leaks are cases when repeated invocation of certain activity creates new set of objects that cannot be reclaimed afteractivity is finished.The biggest problem is accumulation of these objects that leads to increased memory usageand after a long enough time leads to OutOfMemoryError.The nature of this error is that it leaves data structures of an application in undefined stateso anything executed after this moment may lead to unexpected results.

Retained memory is memory occupied by objects that were created to serve some purpose but these objectsare held longer than necessary.This may mean that some action has to be performed that flushes these objects or they will remain in memory until the end of the session.An example of the former is LRU caches (often holding last component in UI, files or projects).A common example of the latter is resources like parsed bundles or images statically referenced in classes that use them.

-J-Dnetbeans.debug.heap can make profiling easier as it more quickly releases references to collapsed nodes.

If you have the Timers module enabled (normally it is in dev builds),click its button in the Memory toolbarto get a summary of interesting live objects and statistics.

Applies to: NetBeans 6.5 and above

Platforms: All

Category:Performance:HowTo

How can I fix memory leaks? (2024)
Top Articles
¿Cuáles son las corrientes de la psicología? - SNHU
7 tư duy cốt lõi của kẻ trí, ắt kiếm bộn tiền, sống cuộc đời cao cấp: Bài học từ hiệu ứng “kiến lười” kinh ngạc!
Dunhams Treestands
Best Big Jumpshot 2K23
Regal Amc Near Me
Blanchard St Denis Funeral Home Obituaries
Summit County Juvenile Court
Irving Hac
Midway Antique Mall Consignor Access
Acbl Homeport
Espn Expert Picks Week 2
Ncaaf Reference
Jet Ski Rental Conneaut Lake Pa
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
Gwdonate Org
Springfield Mo Craiglist
24 Best Things To Do in Great Yarmouth Norfolk
Lonesome Valley Barber
Allentown Craigslist Heavy Equipment
Aps Day Spa Evesham
Sulfur - Element information, properties and uses
Https Paperlesspay Talx Com Boydgaming
Wemod Vampire Survivors
Talk To Me Showtimes Near Marcus Valley Grand Cinema
Prep Spotlight Tv Mn
Barista Breast Expansion
The 15 Best Sites to Watch Movies for Free (Legally!)
Cal State Fullerton Titan Online
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Tom Thumb Direct2Hr
Lilpeachbutt69 Stephanie Chavez
Evil Dead Rise Showtimes Near Regal Sawgrass & Imax
Sony Wf-1000Xm4 Controls
What Is Opm1 Treas 310 Deposit
Dailymotion
FREE Houses! All You Have to Do Is Move Them. - CIRCA Old Houses
Mg Char Grill
Diana Lolalytics
Nearest Ups Office To Me
Hellgirl000
Gvod 6014
Yogu Cheshire
Craigslist Lakeside Az
Emulating Web Browser in a Dedicated Intermediary Box
Ferguson Showroom West Chester Pa
Craigslist Woodward
Ups Authorized Shipping Provider Price Photos
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
Sara Carter Fox News Photos
Myra's Floral Princeton Wv
Minute Clinic Mooresville Nc
Fallout 76 Fox Locations
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5956

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.