凪瀬 Blog
Programming SHOT BAR

目次

Blog 利用状況
  • 投稿数 - 260
  • 記事 - 0
  • コメント - 46948
  • トラックバック - 192
ニュース
広告
  • Java開発者募集中
  • 経歴不問
  • 腕に自信のある方
  • 富山市内
  • (株)凪瀬アーキテクツ
アクセサリ
  • あわせて読みたい
凪瀬悠輝(なぎせ ゆうき)
  • Java技術者
  • お茶好き。カクテル好き。
  • 所属は(株)凪瀬アーキテクツ
  • Twitter:@nagise

書庫

日記カテゴリ

 

@ITより。 Javaのメモリ消費量について調査する際にWindowsのタスクマネージャでJava VMの使用しているメモリ量を 見ても駄目だという話。

メモリリークの調査だとか、使用する資源量の調査だとか、いろいろな理由があってメモリ消費量を 計測したいという話はあちこちで見かけます。ことJavaの場合、メモリはOSから直接供給されるのではなく VMが間に入って管理を行うためOSからプロセスの消費しているメモリ量を見ても実態がつかめないことを まず理解せねばなりません。

オブジェクトがGCされてもメモリ消費量が減らない!と騒ぐ前に

JavaのVMはメモリが必要になったときにその都度OSにメモリを供給してもらうわけではありません

ある程度まとまった量のメモリをOSから貰い、それをプールとして、オブジェクトがnewされるたびに分割して供給するわけです。 そして、オブジェクトの参照がなくなってGC(ガーベッジコレクション)によって回収された場合にも、 プールに戻すだけでOSに細かく返していくわけではありません。

そのため、オブジェクトを作ったり解放したりを繰り返しているJavaプログラムを動かしていても、 Windowsのタスクマネージャから見るとメモリ量は変わらないことがあります。 タスクマネージャから見ているのはVM全体が確保しているメモリだからです。

JavaのVM内でのメモリの使用状況を確認するには

簡素な方法としては、 java.lang.Runtime#freeMemory()を使う手法です。
この値をSystem.out.println()で出力することで、そのときの空き容量を得られますから コードを直接いじりながらデバッグしているような段階では手軽に計測できますね。

ツールを使う場合は jconsole を用います。これはJDKに付属しているツールですので別途ダウンロードする必要はありません。

投稿日時 : 2007年12月4日 15:30
コメント
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    ちゃっぴ
    Posted @ 2007/12/04 15:52
    別に java じゃ無くても process 単位はダメです。
    Task manager
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    凪瀬
    Posted @ 2007/12/04 15:59
    >別にjavaじゃ無くてもprocess単位はダメです。

    単に駄目といわれても混乱を招くだけなので
    何がどう駄目なのか根拠を述べて貰えますでしょうか。
    理由を述べずに「駄目」というのはご遠慮願います。
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    中博俊
    Posted @ 2007/12/04 16:39
    うーん。
    そんなに肩肘張らなくても有名な話で。
    nyaruru メモリ タスクマネージャ

    とかで検索してください。と追補しておきます。

    そもそもマッピングメモリ=物理メモリではないので、Javaの方がメモりの影響は受けやすいですが、Win上で動く物はそこまで神経質になる必要がないんです
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    凪瀬
    Posted @ 2007/12/04 17:30
    ワーキングセット云々の話で論点はあっているのかな?

    私は論点を示さずに否定だけするのは健全な議論を妨げるものと考えていますのでその点だけはご理解ください。
    現に今、「なんで駄目なの?」というやり取りが不毛に行われている。
    ネタ的な会話ならともかく、技術的な会話の中でなんでそんなに論点をぼかしたがるのだろう?

    「Windowsネイティブのアプリケーションでも実際に使っているメモリ量と
    プロセスに割り当てられているメモリ量は違うが、それほど極端に差が出ないので
    概算を知りたいのであればタスクマネージャで見た値を用いても構わない」
    という理解で合っているのでしょうか?
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    凪瀬
    Posted @ 2007/12/04 17:44
    うーん。どうも表現が硬くなるな。
    「駄目とだけ言われてもわからないよ、何が駄目なのか論点を教えてよ」
    ってことなんですよ。

    他のところだったら論拠不明は放置するんですが、
    自分の管理下にある場所ではそうもいかないので。
    ひとつご理解を頂きたい。扱いに困ります…
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    masa
    Posted @ 2007/12/04 22:29
    おお、jconsole。
    これは知りませんでした。勉強になります。
    今後のパフォーマンス測定に有効活用出来そうです。
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    NyaRuRu
    Posted @ 2007/12/05 9:36
    んー,まあ結論として「プログラムの問題点の確認/修正には専用ツール(Javaならjconsole,.NETならCLR Profilerやパフォーマンスカウンタ)を参考にする」なら正論だと思いますのでそれでいいのでは?

    ただ,Windowsのタスクマネージャもそんな単純ではなくて,分厚い解説書を読まなければ数字の意味が把握できない専門家向けツールだと思います.なまじ GUI だし,長いこと Windows を触っている人にとってはお馴染みなので,「正しく理解できている」と誤解されやすいツールですけどね.
    そもそもタスクマネージャの数字で騒いでいる人の場合,経験上,彼らの見ている数字が「VM全体が確保しているメモリですらない」パターンが多いような.

    >「アプリケーションでも実際に使っているメモリ量とプロセスに割り当てられているメモリ量は違うが、それほど極端に差が出ないので概算を知りたいのであればタスクマネージャで見た値を用いても構わない」

    私は割と極端に差が出ると思っています.ワーキングセットですが,私の場合,「使用頻度が高いため物理メモリ上にキャッシュされている領域サイズ」だと考えるようにしています.この量はメモリアクセスの局在性に強く影響を受けるので,「メモリを大量に食う/食わないアルゴリズム」みたいな話には不向きですね.
    以前に書いたものですが,1 MB のピークワーキングセットで 64 MB の配列がソートできましたよという話が以下です.
    http://d.hatena.ne.jp/NyaRuRu/20060620/p1

    でまあ最初の話に戻りますが,何かおかしいと思ったら専用ツールで調べようでいいんじゃないですかね.
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    凪瀬
    Posted @ 2007/12/05 13:05
    なるほど。ありがとうございます。
    「タスクマネージャもそんな単純ではなくて」
    ということなのですね。
    自分も含め、数字の意味を単純に捉えているのが誤りなんだということであれば、
    「別に java じゃ無くても process 単位はダメです。」を納得できます。
    このあたりは私も勉強不足ということですね。

    タスクマネージャでJavaのメモリ使用量を測ろうというのは2重の意味で誤っているという結論でしょうか。
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    NyaRuRu
    Posted @ 2007/12/05 22:09
    > タスクマネージャでJavaのメモリ使用量を測ろうというのは2重の意味で誤っている

    まあ細かく見出すと正解も誤りも曖昧になってくるところはありますが,実行環境である CPU/OS と,抽象化されたアルゴリズムを記述するソースコードでは,メモリという概念の視点が異なるといったところでしょうか.
    タスクマネージャの数字は,実行時に CPU/OS レベルで何が起きているかを把握するのには役立ちますが,元のソースコードがどういうアルゴリズムになっていたかを推測するには不向きです.確かに相関はありますが,原因と結果が一対一に結びつくような単純な応答とは言い難いです.原則としては,「単純に結びつかない」と考えた方が無難というのが持論です.

    あともうひとつおまけです.
    以下試していないので単純な思いつきですが,FileChannel#map で作られる MappedByteBuffer は,java.lang.Runtime#freeMemory() の値よりも巨大なものが作成可能かもしれません.400 MB ぐらいの巨大なファイルで試してみると面白いかも.
    Windows では,ファイルがメモリマップされたアドレスにアクセスを行うと,割り込みを利用して OS が動的に物理メモリを割り当てます.この物理メモリはキャッシュとして働き,しばらくアクセスが行われないと,OS が裏で別用途に転用します.
    そしてWindows XP のタスクマネージャは,このテンポラリに割り当てられた物理メモリも「メモリ使用量」の項目に含めて表示しています.
    仮に 400 MB の MappedByteBuffer を作ったとして,その最初の部分にしかアクセスしないプログラムは,「メモリ使用量」の増加は数 MB にとどまるでしょうし,全体にアクセスすするプログラムでは,「メモリ使用量」は最大 400 MB 程度増加するかもしれません.
    結果として,人々の注目を集めやすい XP の「メモリ使用量」は,実は MappedByteBuffer そのもののサイズでは決まらず,どの位置にどの頻度でアクセスしたかによって大きく変化することになります.(といっても MappedByteBuffer で実際に試したわけではないので,違っているかもしれませんが)

    もっとも,Vista のタスクマネージャでは「Private Working Set」が表示上のデフォルトに変更されたので,400 MBの MappedByteBuffer 全体にアクセスしても,「Private Working Set」はほとんど増えないんじゃないかと思いますが.(あくまで予想です)
    http://d.hatena.ne.jp/NyaRuRu/20060730/p1

    以上は Windows の話ですが,OS が表示する値はあくまで OS 視点だということは,Linux や Solaris でも同じかもしれません.言語解説書ではなかなかそこまで踏み込んで書いてくれませんけどね.
  • # re: タスクマネージャではJavaのメモリ使用量は測れない
    凪瀬
    Posted @ 2007/12/06 12:07
    nioパッケージはあまり研究していなくて私も苦手なところなのですが
    提示のアイデアは非常に興味をそそられますね。
    このあたりの概念はやはりネイティブなOSの知識が必要とされますね…。
    まだまだ勉強しないといけないことが沢山あるな…。

    GCまかせではないメモリの管理手法が可能なのかもしれません。
    そうなると結構革新的かもしれない。
  • # Amazing! Its really awesome piece of writing, I have got much clear idea regarding from this article.
    Amazing! Its really awesome piece of writing, I ha
    Posted @ 2019/01/04 7:41
    Amazing! Its really awesome piece of writing, I have got much clear
    idea regarding from this article.
  • # you are actually a good webmaster. The website loading pace is incredible. It sort of feels that you are doing any unique trick. In addition, The contents are masterwork. you have performed a fantastic process in this topic!
    you are actually a good webmaster. The website loa
    Posted @ 2019/05/30 14:19
    you are actually a good webmaster. The website loading pace is incredible.
    It sort of feels that you are doing any unique trick.
    In addition, The contents are masterwork.
    you have performed a fantastic process in this topic!
  • # I visited various sites but the audio feature for audio songs current at this site is really superb.
    I visited various sites but the audio feature for
    Posted @ 2019/05/30 23:51
    I visited various sites but the audio feature for audio songs current at this
    site is really superb.
  • # Heya! I know this is somewhat off-topic but I needed to ask. Does running a well-established blog like yours take a lot of work? I am brand new to running a blog but I do write in my diary on a daily basis. I'd like to start a blog so I will be able to
    Heya! I know this is somewhat off-topic but I need
    Posted @ 2019/06/04 12:36
    Heya! I know this is somewhat off-topic but I needed to ask.
    Does running a well-established blog like yours
    take a lot of work? I am brand new to running a blog but I do write in my diary on a daily basis.
    I'd like to start a blog so I will be able to share my experience and thoughts
    online. Please let me know if you have any recommendations or tips for new aspiring blog owners.

    Appreciate it!
  • # Asking questions are actually fastidious thing if you are not understanding something completely, except this piece of writing gives good understanding even.
    Asking questions are actually fastidious thing if
    Posted @ 2019/06/06 20:03
    Asking questions are actually fastidious thing if you are not understanding something
    completely, except this piece of writing gives good understanding even.
  • # Hey there! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any recommendations?
    Hey there! Do you know if they make any plugins to
    Posted @ 2019/06/07 19:04
    Hey there! Do you know if they make any plugins to safeguard against hackers?

    I'm kinda paranoid about losing everything I've worked hard on. Any recommendations?
  • # Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
    Wonderful, what a website it is! This weblog prese
    Posted @ 2019/07/18 9:13
    Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
  • # Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
    Wonderful, what a website it is! This weblog prese
    Posted @ 2019/07/18 9:14
    Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
  • # Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
    Wonderful, what a website it is! This weblog prese
    Posted @ 2019/07/18 9:15
    Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
  • # Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
    Wonderful, what a website it is! This weblog prese
    Posted @ 2019/07/18 9:16
    Wonderful, what a website it is! This weblog presents helpful facts to us, keep it up.
  • # I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
    I am regular visitor, how are you everybody? This
    Posted @ 2019/09/06 21:47
    I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
  • # I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
    I am regular visitor, how are you everybody? This
    Posted @ 2019/09/06 21:48
    I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
  • # I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
    I am regular visitor, how are you everybody? This
    Posted @ 2019/09/06 21:49
    I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
  • # I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
    I am regular visitor, how are you everybody? This
    Posted @ 2019/09/06 21:50
    I am regular visitor, how are you everybody? This post posted at this site is really fastidious.
  • # Illikebuisse rlyzf
    pharmaceptica.com
    Posted @ 2021/07/04 12:07
    sildenafil versus tadalafil https://pharmaceptica.com/
  • # re: ??????????Java????????????
    side effects hydroxychloroquine
    Posted @ 2021/07/07 3:32
    chrloroquine https://chloroquineorigin.com/# hydroxychlor tab 200mg
  • # re: ??????????Java????????????
    hydrochloquine
    Posted @ 2021/07/13 3:40
    cloroquin https://chloroquineorigin.com/# what is hydroxychloroquine used for arthritis
  • # bqiscxtmjgip
    dwedaylzqj
    Posted @ 2021/12/04 7:50
    https://chloroquinesand.com/
  • # Свежие новости
    Adampyq
    Posted @ 2022/02/20 23:00
    Где Вы ищите свежие новости?
    Лично я читаю и доверяю газете https://www.ukr.net/.
    Это единственный источник свежих и независимых новостей.
    Рекомендую и Вам
  • # Amazing! Its truly remarkable post, I have got much clear idea concerning from this article.
    Amazing! Its truly remarkable post, I have got muc
    Posted @ 2022/03/23 1:38
    Amazing! Its truly remarkable post, I have got much
    clear idea concerning from this article.
  • # Amazing! Its truly remarkable post, I have got much clear idea concerning from this article.
    Amazing! Its truly remarkable post, I have got muc
    Posted @ 2022/03/23 1:39
    Amazing! Its truly remarkable post, I have got much
    clear idea concerning from this article.
  • # Amazing! Its truly remarkable post, I have got much clear idea concerning from this article.
    Amazing! Its truly remarkable post, I have got muc
    Posted @ 2022/03/23 1:40
    Amazing! Its truly remarkable post, I have got much
    clear idea concerning from this article.
  • # Amazing! Its truly remarkable post, I have got much clear idea concerning from this article.
    Amazing! Its truly remarkable post, I have got muc
    Posted @ 2022/03/23 1:41
    Amazing! Its truly remarkable post, I have got much
    clear idea concerning from this article.
  • # Hi, always i used to check blog posts here in the early hours in the morning, for the reason that i love to gain knowledge of more and more.
    Hi, always i used to check blog posts here in the
    Posted @ 2022/03/24 5:32
    Hi, always i used to check blog posts here
    in the early hours in the morning, for the reason that i love to gain knowledge of more and more.
  • # I'm really enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work!
    I'm really enjoying the design and layout of your
    Posted @ 2022/06/05 6:24
    I'm really enjoying the design and layout of your website.
    It's a very easy on the eyes which makes it much more enjoyable
    for me to come here and visit more often. Did you hire
    out a designer to create your theme? Superb work!
  • # I am genuinely thankful to the owner of this web site who has shared this impressive article at at this place.
    I am genuinely thankful to the owner of this web s
    Posted @ 2022/06/06 13:50
    I am genuinely thankful to the owner of this web site who has shared
    this impressive article at at this place.
  • # Hi there! Do you know if they make any plugins to assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success. If you know of any please share. Appreciate it!
    Hi there! Do you know if they make any plugins to
    Posted @ 2022/06/06 17:15
    Hi there! Do you know if they make any plugins to assist with SEO?
    I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success.

    If you know of any please share. Appreciate it!
  • # My developer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he's tryiong none the less. I've been using WordPress on various websites for about a year and am concerned about switching to
    My developer is trying to persuade me to move to .
    Posted @ 2022/06/08 6:53
    My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the
    expenses. But he's tryiong none the less. I've been using WordPress on various websites for about a year and
    am concerned about switching to another platform.
    I have heard excellent things about blogengine.net.
    Is there a way I can transfer all my wordpress posts into it?
    Any help would be greatly appreciated!
  • # Hi to every one, because I am really eager of reading this web site's post to be updated daily. It contains good material.
    Hi to every one, because I am really eager of read
    Posted @ 2022/06/10 21:42
    Hi to every one, because I am really eager of reading this web site's post to be updated daily.

    It contains good material.
  • # You need to take part in a contest for one of the most useful sites on the internet. I most certainly will recommend this blog!
    You need to take part in a contest for one of the
    Posted @ 2022/06/11 8:57
    You need to take part in a contest for one of the most useful sites on the internet.
    I most certainly will recommend this blog!
  • # WOW just what I was looking for. Came here by searching for C#
    WOW just what I was looking for. Came here by sea
    Posted @ 2022/08/03 19:14
    WOW just what I was looking for. Came here by searching for C#
  • # WOW just what I was looking for. Came here by searching for C#
    WOW just what I was looking for. Came here by sea
    Posted @ 2022/08/03 19:16
    WOW just what I was looking for. Came here by searching for C#
  • # WOW just what I was looking for. Came here by searching for C#
    WOW just what I was looking for. Came here by sea
    Posted @ 2022/08/03 19:17
    WOW just what I was looking for. Came here by searching for C#
  • # We stumbled over here different website and thought I might check things out. I like what I see so now i am following you. Look forward to checking out your web page yet again.
    We stumbled over here different website and thoug
    Posted @ 2022/08/10 16:16
    We stumbled over here different website and thought I might
    check things out. I like what I see so now i am following you.
    Look forward to checking out your web page yet again.
  • # It's a pity you don't have a donate button! I'd most certainly donate to this fantastic blog! I guess for now i'll settle for bookmarking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this blog with m
    It's a pity you don't have a donate button! I'd mo
    Posted @ 2022/08/12 9:27
    It's a pity you don't have a donate button! I'd most certainly donate to this fantastic blog!
    I guess for now i'll settle for bookmarking and adding your RSS feed to my Google account.

    I look forward to new updates and will talk about this blog with my Facebook group.
    Talk soon!
  • # You should be a part of a contest for one of the best blogs on the web. I'm going to recommend this blog!
    You should be a part of a contest for one of the b
    Posted @ 2022/08/18 9:39
    You should be a part of a contest for one of
    the best blogs on the web. I'm going to recommend this blog!
  • # Really no matter if someone doesn't understand after that its up to other users that they will assist, so here it takes place.
    Really no matter if someone doesn't understand aft
    Posted @ 2022/08/19 14:13
    Really no matter if someone doesn't understand after
    that its up to other users that they will assist, so here it takes place.
  • # hello!,I like your writing very much! share we keep up a correspondence extra about your article on AOL? I need an expert in this area to resolve my problem. Maybe that is you! Having a look ahead to peer you.
    hello!,I like your writing very much! share we kee
    Posted @ 2022/11/26 10:26
    hello!,I like your writing very much! share we keep up a correspondence extra about your article on AOL?
    I need an expert in this area to resolve my problem.
    Maybe that is you! Having a look ahead to peer you.
  • # doors2.txt;1
    BFQUiKuuNh
    Posted @ 2023/03/14 14:55
    doors2.txt;1
  • # Hi there, I log on to your new stuff daily. Your writing style is witty, keep doing what you're doing!
    Hi there, I log on to your new stuff daily. Your
    Posted @ 2024/04/02 14:41
    Hi there, I log on to your new stuff daily. Your writing style is witty, keep doing what you're doing!
  • # Hi there, I log on to your new stuff daily. Your writing style is witty, keep doing what you're doing!
    Hi there, I log on to your new stuff daily. Your
    Posted @ 2024/04/02 14:42
    Hi there, I log on to your new stuff daily. Your writing style is witty, keep doing what you're doing!
  • # My brother suggested I might like this web site. He was once entirely right. This publish actually made my day. You cann't believe simply how much time I had spent for this information! Thanks!
    My brother suggested I might like this web site. H
    Posted @ 2024/04/12 2:24
    My brother suggested I might like this web
    site. He was once entirely right. This publish actually made my day.
    You cann't believe simply how much time I had spent for this information! Thanks!
  • # I couldn't refrain from commenting. Perfectly written!
    I couldn't refrain from commenting. Perfectly writ
    Posted @ 2024/04/26 5:48
    I couldn't refrain from commenting. Perfectly written!
  • # Great information. Lucky me I recently found your website by chance (stumbleupon). I've book-marked it for later!
    Great information. Lucky me I recently found your
    Posted @ 2024/05/03 4:56
    Great information. Lucky me I recently found your website
    by chance (stumbleupon). I've book-marked it for later!
  • # Great information. Lucky me I recently found your website by chance (stumbleupon). I've book-marked it for later!
    Great information. Lucky me I recently found your
    Posted @ 2024/05/03 4:57
    Great information. Lucky me I recently found your website
    by chance (stumbleupon). I've book-marked it for later!
  • # Great information. Lucky me I recently found your website by chance (stumbleupon). I've book-marked it for later!
    Great information. Lucky me I recently found your
    Posted @ 2024/05/03 4:58
    Great information. Lucky me I recently found your website
    by chance (stumbleupon). I've book-marked it for later!
  • # Great information. Lucky me I recently found your website by chance (stumbleupon). I've book-marked it for later!
    Great information. Lucky me I recently found your
    Posted @ 2024/05/03 4:58
    Great information. Lucky me I recently found your website
    by chance (stumbleupon). I've book-marked it for later!
タイトル
名前
Url
コメント